Package cn.sliew.milky.cache.lettuce
Class LettuceCache<K,V>
- java.lang.Object
-
- cn.sliew.milky.cache.lettuce.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是如何操作的。
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classLettuceCache.ValueWrapper<V>
-
Constructor Summary
Constructors Constructor Description LettuceCache(LettuceCacheOptions<K,V> options)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclear()VcomputeIfAbsent(K key, CacheLoader<K,V> loader, Duration expire)booleancontainsKey(K key)voiddestroy()Vget(K key)Iterator<K>hotKeyIterator(int n)Iterator<K>keyIterator()fixme hkeys hscan todo 考虑使用sortset的key来搞。zscanStringname()voidput(K key, V value)voidput(K key, V value, Duration expire)voidremove(K key)voidremoveAll(Iterable<K> keys)longsize()voidstats(io.micrometer.core.instrument.MeterRegistry registry)fixme 数据监控booleansupportNullKey()booleansupportNullValue()
-
-
-
Constructor Detail
-
LettuceCache
public LettuceCache(LettuceCacheOptions<K,V> options)
-
-
Method Detail
-
containsKey
public boolean containsKey(K key)
- Specified by:
containsKeyin interfaceCache<K,V>
-
computeIfAbsent
public V computeIfAbsent(K key, CacheLoader<K,V> loader, Duration expire)
- Specified by:
computeIfAbsentin interfaceCache<K,V>
-
supportNullKey
public boolean supportNullKey()
- Specified by:
supportNullKeyin interfaceCache<K,V>
-
supportNullValue
public boolean supportNullValue()
- Specified by:
supportNullValuein interfaceCache<K,V>
-
keyIterator
public Iterator<K> keyIterator()
fixme hkeys hscan todo 考虑使用sortset的key来搞。zscan- Specified by:
keyIteratorin interfaceCache<K,V>
-
hotKeyIterator
public Iterator<K> hotKeyIterator(int n)
- Specified by:
hotKeyIteratorin interfaceCache<K,V>
-
stats
public void stats(io.micrometer.core.instrument.MeterRegistry registry)
fixme 数据监控
-
-