自定义缓存

随便记记

service

public Object getSessionAttribute(HttpServletRequest request,String key){
		CookieCache cache = (CookieCache)request.getAttribute("CACHE");
		if(cache != null){
			return cache.getCache(key);
		}
		return null;
	}
	
	public void removeSessionAttribute(HttpServletRequest request,HttpServletResponse response, String key){
		CookieCache cache = (CookieCache)request.getAttribute("CACHE");
		if(cache != null){
			cache.removeCache(key);
		}
	}
	
	public void addSessionAttribute(HttpServletRequest request,HttpServletResponse response, String key,Object val){
		CookieCache cache = (CookieCache)request.getAttribute("CACHE");
		if(cache != null){
			cache.pushCache(key, val);
		}else{
			cache = new CookieCache();
			cache.pushCache(key, val);
			cache.setBindIp(request.getRemoteAddr());
			request.setAttribute("CACHE", cache);
			
		}
	}

bean

public class CookieCache implements java.io.Serializable{

	private static final long serialVersionUID = 246613021579031321L;

	private int randomId = new java.util.Random(System.currentTimeMillis()).nextInt();
	
	public String bindIp;
	
	private Map<String,Object> cache = new HashMap<String,Object>();
	
	/**
	 * @return the bindIp
	 */
	public String getBindIp() {
		return bindIp;
	}

	/**
	 * @return the randomId
	 */
	public int getRandomId() {
		return randomId;
	}



	/**
	 * @param randomId the randomId to set
	 */
	public void setRandomId(int randomId) {
		this.randomId = randomId;
	}



	/**
	 * @return the cache
	 */
	public Map<String, Object> getCache() {
		return cache;
	}



	/**
	 * @param cache the cache to set
	 */
	public void setCache(Map<String, Object> cache) {
		this.cache = cache;
	}



	/**
	 * @param bindIp the bindIp to set
	 */
	public void setBindIp(String bindIp) {
		this.bindIp = bindIp;
	}



	public void pushCache(String key,Object val){
		cache.put(String.format("%d%s", this.randomId,key), val);
	}
	
	public Object removeCache(String key){
		return cache.remove(String.format("%d%s", this.randomId,key));
	}
	
	public Object getCache(String key){
		return cache.get(String.format("%d%s", this.randomId,key));
	}
	
	@Override
	public String toString() {
		return JSONObject.fromObject(this).toString();
	}
}
然后通过拦截器进行迭代更新,




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MybatisPlus提供了自定义Redis缓存的功能,可以通过自定义Redis缓存实现数据的快速查询。 1. 引入Redis依赖 ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> ``` 2. 自定义缓存 ```java @Component public class RedisCache implements Cache { private final RedisTemplate<Object, Object> redisTemplate; private final String cacheName; private final long ttl; public RedisCache(RedisTemplate<Object, Object> redisTemplate, String cacheName) { this.redisTemplate = redisTemplate; this.cacheName = cacheName; this.ttl = 60 * 5; //默认5分钟过期时间 } public RedisCache(RedisTemplate<Object, Object> redisTemplate, String cacheName, long ttl) { this.redisTemplate = redisTemplate; this.cacheName = cacheName; this.ttl = ttl; } @Override public String getId() { return cacheName; } @Override public void putObject(Object key, Object value) { redisTemplate.opsForValue().set(key, value, ttl, TimeUnit.SECONDS); } @Override public Object getObject(Object key) { return redisTemplate.opsForValue().get(key); } @Override public Object removeObject(Object key) { redisTemplate.delete(key); return null; } @Override public void clear() { redisTemplate.delete(redisTemplate.keys("*" + cacheName + "*")); } @Override public int getSize() { return Math.toIntExact(redisTemplate.keys("*" + cacheName + "*").size()); } @Override public ReadWriteLock getReadWriteLock() { return null; } } ``` 3. 注册自定义缓存 ```java @Configuration @EnableCaching public class CacheConfig extends CachingConfigurerSupport { private final RedisConnectionFactory redisConnectionFactory; public CacheConfig(RedisConnectionFactory redisConnectionFactory) { this.redisConnectionFactory = redisConnectionFactory; } @Bean public CacheManager cacheManager() { RedisCacheConfiguration defaultCacheConfig = RedisCacheConfiguration.defaultCacheConfig() .entryTtl(Duration.ofMinutes(10)) .disableCachingNullValues(); RedisCacheConfiguration userCacheConfig = RedisCacheConfiguration.defaultCacheConfig() .entryTtl(Duration.ofMinutes(5)) .disableCachingNullValues() .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer())); Map<String, RedisCacheConfiguration> cacheConfigurations = new HashMap<String, RedisCacheConfiguration>() {{ put("user", userCacheConfig); }}; RedisCacheManager cacheManager = RedisCacheManager.builder(redisConnectionFactory) .cacheDefaults(defaultCacheConfig) .withInitialCacheConfigurations(cacheConfigurations) .build(); return cacheManager; } @Bean @Override public CacheResolver cacheResolver() { return new SimpleCacheResolver(cacheManager()); } @Bean @Override public CacheErrorHandler errorHandler() { return new SimpleCacheErrorHandler(); } @Bean public Cache redisCache(RedisTemplate<Object, Object> redisTemplate) { return new RedisCache(redisTemplate, "product"); } } ``` 4. 使用自定义缓存 在Mapper接口方法上添加@Cacheable注解,指定使用自定义的Redis缓存即可。 ```java @Mapper public interface UserMapper extends BaseMapper<User> { @Cacheable(value = "user", key = "#id") User getById(Long id); } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值