java手写多级缓存
2019-08-16 09:48:51来源:博客园 阅读 ()
java手写多级缓存
多级缓存实现类,时间有限,该类未抽取接口,目前只支持两级缓存:JVM缓存(实现 请查看上一篇:java 手写JVM高性能缓存)、redis缓存(在spring 的 redisTemplate 基础实现)
package com.ws.commons.cache; import java.util.function.Supplier; import com.ws.commons.tool.ThreadTool; /** * 多级缓存实现 * * @author 尘无尘 * */ public class MultilevelCache { private MultilevelCache() { } private static final ICache FIRST_LEVE_LCACHE = LocalCache.instance(); private static ICache secondCache; private static final String LOCK_PREFIX = "MUILCACHE_LOCK:"; public static synchronized void init(ICache secondCache) { if (MultilevelCache.secondCache == null) { MultilevelCache.secondCache = secondCache; } } public static void put(String key, Object value, int timeOutSecond) { FIRST_LEVE_LCACHE.put(key, value, timeOutSecond); if (secondCache != null) { secondCache.put(key, value, timeOutSecond); } } /** * 提供数据,并缓存 * * @param key * @param supplier * @return */ @SuppressWarnings("unchecked") public static <T> T getForload(String key, Supplier<T> supplier) { T data = FIRST_LEVE_LCACHE.get(key); if (data == null && secondCache != null) { data = (T) secondCache.get(key); } if (data != null) { return data; } synchronized (ThreadTool.buildLock(LOCK_PREFIX, key)) { data = FIRST_LEVE_LCACHE.get(key); if (data == null && secondCache != null) { data = (T) secondCache.get(key); } if (data != null) { return data; } data = supplier.get(); if (secondCache != null) { secondCache.put(key, data); } FIRST_LEVE_LCACHE.put(key, data); } return data; } /** * 提供数据,并缓存一定的时间 * * @param key * @param timeOutSecond * @param supplier * @return */ @SuppressWarnings("unchecked") public static <T> T getForload(String key, int timeOutSecond, Supplier<T> supplier) { T data = FIRST_LEVE_LCACHE.get(key); if (data == null && secondCache != null) { data = (T) secondCache.get(key); } if (data != null) { return data; } synchronized (ThreadTool.buildLock(LOCK_PREFIX, key)) { data = FIRST_LEVE_LCACHE.get(key); if (data == null && secondCache != null) { data = (T) secondCache.get(key); } if (data != null) { return data; } data = supplier.get(); if (secondCache != null) { secondCache.put(key, data, timeOutSecond); } FIRST_LEVE_LCACHE.put(key, data, timeOutSecond); } return data; } @SuppressWarnings("unchecked") public static <T> T removeAndGet(String key) { T data = null; if (secondCache != null) { data = (T) secondCache.removeAndGet(key); } T data2 = FIRST_LEVE_LCACHE.removeAndGet(key); if (data == null) { data = data2; } return data; } public static void remove(String key) { if (secondCache != null) { secondCache.remove(key); } FIRST_LEVE_LCACHE.remove(key); } @SuppressWarnings("unchecked") public static <T> T get(String key) { T data = FIRST_LEVE_LCACHE.get(key); if (data == null && secondCache != null) { data = (T) secondCache.get(key); } return data; } public static void expire(String key, int timeOutSecond) { FIRST_LEVE_LCACHE.expire(key, timeOutSecond); if (secondCache != null) { secondCache.expire(key, timeOutSecond); } } }
redis缓存实现类
package com.walmart.cirular.interfaces.application; import java.util.concurrent.TimeUnit; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Component; import com.ws.commons.cache.ICache; /** * redis 缓存实现类 * * @author 尘无尘 * */ @Component public class RedisICacheImpl implements ICache { @Autowired(required = false) private RedisTemplate<String, Object> redisTemplate; @Override public void put(String key, Object value) { redisTemplate.opsForValue().set(key, value); } @Override public void put(String key, Object value, int timeOutSecond) { redisTemplate.opsForValue().set(key, value, (long) timeOutSecond, TimeUnit.SECONDS); } @SuppressWarnings("unchecked") @Override public <T> T get(String key) { T t = (T) redisTemplate.opsForValue().get(key); redisTemplate.delete(key); return t; } @Override public void remove(String key) { redisTemplate.delete(key); } @SuppressWarnings("unchecked") @Override public <T> T removeAndGet(String key) { Object obj = get(key); redisTemplate.delete(key); return (T) obj; } @Override public void rightPush(String key, Object value, int timeOutSecond) { redisTemplate.opsForList().rightPush(key, value); redisTemplate.expire(key, timeOutSecond, TimeUnit.SECONDS); } @SuppressWarnings("unchecked") @Override public <T> T rightPop(String key) { return (T) redisTemplate.opsForList().rightPop(key); } @SuppressWarnings("unchecked") @Override public <T> T leftPop(String key) { return (T) redisTemplate.opsForList().leftPop(key); } @Override public void leftPush(String key, Object value) { redisTemplate.opsForList().leftPush(key, value); } @Override public void rightPush(String key, Object value) { redisTemplate.opsForList().rightPush(key, value); } @Override public void expire(String key, int timeOutSecond) { redisTemplate.expire(key, timeOutSecond, TimeUnit.SECONDS); } @Override public boolean hasKey(String key) { return redisTemplate.hasKey(key); } @Override public boolean putIfAbsent(String key, Object value) { return redisTemplate.opsForValue().setIfAbsent(key, value); } @Override public boolean putIfAbsent(String key, Object value, int timeOutSecond) { boolean flag = putIfAbsent(key, value); if (flag) { expire(key, timeOutSecond); } return flag; } }
原文链接:https://www.cnblogs.com/abab/p/11139477.html
如有疑问请与原作者联系
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- 国外程序员整理的Java资源大全(全部是干货) 2020-06-12
- 2020年深圳中国平安各部门Java中级面试真题合集(附答案) 2020-06-11
- 2020年java就业前景 2020-06-11
- 04.Java基础语法 2020-06-11
- Java--反射(框架设计的灵魂)案例 2020-06-11
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash