• Redis淘汰策略及LRU算法


    一.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());
    
        }
    }

  • 相关阅读:
    Android Activity
    As3 3D学习之rotation
    Android学习之Android自带例子 ContactManager
    Android学习之 Menu
    Android学习之 Intent
    VC++学习之建立窗口
    As3 3D学习之屏幕与3D坐标
    Android学习之 Button onClickListener实现方法
    ClientDataSet 心得
    部分系统路径
  • 原文地址:https://www.cnblogs.com/liuyi13535496566/p/16394253.html
Copyright © 2020-2023  润新知