一.Redis查看内存大小
1.查看配置文件,里面找 maxmemory ,如果注释或者配置0,64位默认不限制,32位为3g
2.命令方式: info memory
二.Redis满了继续存数据会出现什么异常?
报OOM
三.Redis的删除策略
1.定时删除:对cpu不友好,用处理器性能换取存储时间,(拿时间换空间)
2.惰性删除: 对内存不友好,用存储空间换取处理器性能(拿空间换时间)
3.定期删除
八种淘汰策略:
一般会选择allkeys-lru 淘汰策略.
public class LRUCacheDemo<K,V> extends LinkedHashMap<K,V> { private int capacity;//缓存坑位 public LRUCacheDemo(int capacity) { super(capacity, 0.75F, true); this.capacity = capacity; } protected boolean removeEldestEntry(Map.Entry<K,V> eldest) { return super.size() > capacity; } public static void main(String[] args) { LRUCacheDemo lruCacheDemo = new LRUCacheDemo(3); lruCacheDemo.put(1,"a"); lruCacheDemo.put(2,"b"); lruCacheDemo.put(3,"c"); System.out.println(lruCacheDemo.keySet()); lruCacheDemo.put(4,"d"); System.out.println(lruCacheDemo.keySet()); lruCacheDemo.put(3,"c"); System.out.println(lruCacheDemo.keySet()); lruCacheDemo.put(3,"c"); System.out.println(lruCacheDemo.keySet()); lruCacheDemo.put(3,"c"); System.out.println(lruCacheDemo.keySet()); lruCacheDemo.put(5,"x"); System.out.println(lruCacheDemo.keySet()); } }
public class LRUCacheDemo<K,V> extends LinkedHashMap<K,V> { private int capacity;//缓存坑位 public LRUCacheDemo(int capacity) { super(capacity, 0.75F, false); this.capacity = capacity; } protected boolean removeEldestEntry(Map.Entry<K,V> eldest) { return super.size() > capacity; } public static void main(String[] args) { LRUCacheDemo lruCacheDemo = new LRUCacheDemo(3); lruCacheDemo.put(1,"a"); lruCacheDemo.put(2,"b"); lruCacheDemo.put(3,"c"); System.out.println(lruCacheDemo.keySet()); lruCacheDemo.put(4,"d"); System.out.println(lruCacheDemo.keySet()); lruCacheDemo.put(3,"c"); System.out.println(lruCacheDemo.keySet()); lruCacheDemo.put(3,"c"); System.out.println(lruCacheDemo.keySet()); lruCacheDemo.put(3,"c"); System.out.println(lruCacheDemo.keySet()); lruCacheDemo.put(5,"x"); System.out.println(lruCacheDemo.keySet()); } }