首页 > 编程知识 正文

springboot缓存过期时间,spring缓存注解Redis

时间:2023-05-04 14:34:44 阅读:17520 作者:2300

解决redis乱码

package com.example.demo.kang.config; importorg.spring framework.beans.factory.annotation.value; importorg.spring framework.boot.auto configure.cache.cache properties; importorg.spring帧work.context.annotation.bean; importorg.spring framework.context.annotation.configuration; importorg.spring framework.data.redis.cache.rediscacheconfiguration; importorg.spring帧. data.redis.serializer.*;/* * @ author Chen kang */@ configuration publicclassredisconfig { @ value ($ { spring.redis.host } ' ) privatestring @ vvior @ value ($ { spring.redis.password } ) private String password; @ beanpublicrediscacheconfigurationrediscacheconfiguration (cachepropertiescacheproperties ) cache properties.redisredispropropre onfig=rediscacheconfiguration.defaultcacheconfig (; config=config.serializevalueswith (redisserializationcontext.serialization pair.from serializer (newgenericjackson 2js OS=null (config=config.entry TTL (redis properties.gettimetolive () ); (if ) redisproperties.getkeyprefix (!=null () config=config.prefixkeyswith (redis properties.getkey prefix ) ); (if (! redis properties.iscachenullvalues () ) config=config.disablecachingnullvalues ); (if (! redisProperties.isUseKeyPrefix () { config=config.disablekeyprefix ); }返回配置; }测试:

如果调用了//方法,则首先检查cache中是否有对该方法的同一参数的调用,如果有,则从cache进行查询并返回结果。 如果没有,则运行具体的方法逻辑并将结果缓存到cache中。 @ request mapping (value=' test cache ',method=RequestMethod.GET ) cacheable ) cachenames='kang_cache_test' //method=RequestMethod.GET ) cacheevict ) cachenames='kang_cacct key='#s ' ) publicbooleandetetext (@ request parart 标记为//方法,先执行该方法,然后使用方法返回的值进行缓存内容@ request mapping (value=' put cache ', 更新method=RequestMethod.GET ) cache put (cache names )的key='#s'(publicstringputtext ) requestparam(value='s ) }//@Caching//复杂的cache配置。 可以将gxdfn配置上的一些注释//// @CacheConfig//放在类中,以便在类中的缓存操作中统一配置关键字。

cacheNames/value :指定缓存组件的名称; 方法的返回结果放在哪个缓存中,可以通过数组的方式指定多个缓存;

key :用于缓存数据的key; 你可以用那个指定。 缺省情况下,使用方法参数的值1-方法的返回值编写SpEL; #i d; 参数id的值#a0 #p0 #root.args[0] getEmp[2]

key生成器: key生成器; 您可以自己指定key生成器的组件id。 key/keyGenerator :双选择使用;

缓存管理器:指定缓存管理器; 或cacheResolver指定解析器的获取

condition :指定仅在满足条件时缓存; condition=“#a01”:仅当第一个参数的值为“1”时缓存

unless:否定缓存; 如果在unless中指定的条件为true,则不缓存方法的返回值。 可以得到结果进行判断

为什么源代码:定位于redis

将加载redisCacheConfiguration

cacheManager正在加载

用@cache拦截器拦截,获取对应的manager后,即可得到redis的配置

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。