Class LettuceCache<K,​V>

  • All Implemented Interfaces:
    Cache<K,​V>

    public class LettuceCache<K,​V>
    extends Object
    implements Cache<K,​V>
    redis缓存没有使用常用的strings类型来搞 使用了hash作为缓存存储模式带来一个问题:redis官方至今没有打算支持hash的field的ttl。 因此需要自己实现针对hash的field的ttl。 为value对象创建包装类ValueWrapper,记录过期时间戳。获取缓存对象的时候查看对象是否已经过期。 对于每个key对象放入一个sortset,以过期时间戳作为分数,创建定时任务定期从sortset和hash 中移除缓存对象。 为了保证数据不会出现永久停留在redis中的问题,设置缓存的过期时间统一为凌晨3点,缓存的凌晨过期 不会造成业务的突然抖动,也能定时清理所有的缓存对象 todo 考虑到对象双写的问题,需要将同时操作sortset和hash的对象移除功能处理为一个 todo 原子操作,需要使用lua脚本来搞。

    时间轮是一秒跳一次,每个任务执行完毕后设置的是一秒后执行下一个任务,因此最快两个任务的执行间隔为2s。 回头需要研究下spring中的redis cache是如何操作的。

    • Method Detail

      • get

        public V get​(K key)
        Specified by:
        get in interface Cache<K,​V>
      • containsKey

        public boolean containsKey​(K key)
        Specified by:
        containsKey in interface Cache<K,​V>
      • put

        public void put​(K key,
                        V value)
        Specified by:
        put in interface Cache<K,​V>
      • put

        public void put​(K key,
                        V value,
                        Duration expire)
        Specified by:
        put in interface Cache<K,​V>
      • remove

        public void remove​(K key)
        Specified by:
        remove in interface Cache<K,​V>
      • size

        public long size()
        Specified by:
        size in interface Cache<K,​V>
      • supportNullKey

        public boolean supportNullKey()
        Specified by:
        supportNullKey in interface Cache<K,​V>
      • keyIterator

        public Iterator<K> keyIterator()
        fixme hkeys hscan todo 考虑使用sortset的key来搞。zscan
        Specified by:
        keyIterator in interface Cache<K,​V>
      • stats

        public void stats​(io.micrometer.core.instrument.MeterRegistry registry)
        fixme 数据监控
        Specified by:
        stats in interface Cache<K,​V>
      • clear

        public void clear()
        Specified by:
        clear in interface Cache<K,​V>
      • destroy

        public void destroy()
        Specified by:
        destroy in interface Cache<K,​V>