RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
springboot中怎么操作redis缓存

springboot中怎么操作redis缓存,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。

创新互联是专业的资兴网站建设公司,资兴接单;提供成都网站设计、成都网站建设,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行资兴网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!

引入依赖库

在pom中引入依赖库,如下

org.springframework.boot spring-boot-starter-data-redis redis.clients jedis

注解使用

@Cacheable@Cacheable("product")@Cacheable(value = {"product","order"}, key = "#root.targetClass+'-'+#id")@Cacheable(value = "product", key = "#root.targetClass+'-'+#id")

自定义cacheManager

@Cacheable(value = "product", key = "#root.targetClass+'-'+#id” cacheManager="cacheManager")@CachePut

应用到写数据的方法上,如新增/修改方法

@CachePut(value = "product", key = "#root.targetClass+'-'+#product.id")@CacheEvict

即应用到移除数据的方法上,如删除方法

@CacheEvict(value = "product", key = "#root.targetClass+'-'+#id")

提供的SpEL上下文数据

Spring Cache提供了一些供我们使用的SpEL上下文数据,下表直接摘自Spring官方文档:

methodName      root对象      当前被调用的方法名      #root.methodName              method      root对象      当前被调用的方法      #root.method.name              target      root对象      当前被调用的目标对象      #root.target              targetClass      root对象      当前被调用的目标对象类      #root.targetClass              args      root对象      当前被调用的方法的参数列表      #root.args[0]              caches      root对象      当前方法调用使用的缓存列表(如@Cacheable(value={"cache1", "cache2"})),则有两个cache      #root.caches[0].name              argument name      执行上下文      当前被调用的方法的参数,如findById(Long id),我们可以通过#id拿到参数      #user.id              result      执行上下文      方法执行后的返回值(仅当方法执行之后的判断有效,如‘unless','cache evict'的beforeInvocation=false)      #result

自定义Cache配置

@Configuration@EnableCachingpublic class RedisConfig extends CachingConfigurerSupport { /** * 自定义redis key值生成策略 */ @Bean @Override public KeyGenerator keyGenerator() {  return (target, method, params) -> {   StringBuilder sb = new StringBuilder();   sb.append(target.getClass().getName());   sb.append(method.getName());   for (Object obj : params) {    sb.append(obj.toString());   }   return sb.toString();  }; } @Bean public RedisTemplate redisTemplate(RedisConnectionFactory factory) {  ObjectMapper om = new ObjectMapper();  om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);  om.enableDefaultTyping(Object

名字位置描述示例

看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注创新互联行业资讯频道,感谢您对创新互联的支持。


网站题目:springboot中怎么操作redis缓存
网站URL:http://cqwzjz.cn/article/gioecd.html