您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

spring中redis怎么用

2025/9/30 14:53:02发布18次查看
spring中redis怎么用?
在spring中使用redis
java中操作redis使用的是jedis,首先在pom.xml中加入相关依赖:
<!-- redis cache related.....start --><dependency><groupid>org.springframework.data</groupid><artifactid>spring-data-redis</artifactid><version>1.6.0.release</version></dependency><dependency><groupid>redis.clients</groupid><artifactid>jedis</artifactid><version>2.7.3</version></dependency><!-- https://mvnrepository.com/artifact/org.apache.commons/commons-pool2 --><dependency> <groupid>org.apache.commons</groupid> <artifactid>commons-pool2</artifactid> <version>2.4.2</version></dependency><!-- redis cache related.....end -->
然后实现配置类:
package com.ehelp.util; import org.springframework.cache.cachemanager;import org.springframework.cache.annotation.cachingconfigurersupport;import org.springframework.cache.annotation.enablecaching;import org.springframework.context.annotation.bean;import org.springframework.context.annotation.configuration;import org.springframework.data.redis.cache.rediscachemanager;import org.springframework.data.redis.connection.redisconnectionfactory;import org.springframework.data.redis.connection.jedis.jedisconnectionfactory;import org.springframework.data.redis.core.redistemplate; @configuration@enablecachingpublic class rediscacheconfig extends cachingconfigurersupport { @beanpublic jedisconnectionfactory redisconnectionactory() {jedisconnectionfactory redisconnectionfactory = new jedisconnectionfactory();redisconnectionfactory.sethostname("localhost");redisconnectionfactory.setport(6379);return redisconnectionfactory;} @beanpublic redistemplate<string, string> redistemplate(redisconnectionfactory cf) {redistemplate<string, string> redistemplate = new redistemplate<string, string>();redistemplate.setconnectionfactory(cf);return redistemplate;}@beanpublic cachemanager cachemanager(redistemplate redistemplate) {rediscachemanager cachemanger = new rediscachemanager(redistemplate);cachemanger.setdefaultexpiration(5); //cache过期时间return cachemanger;}}
注意:
设置 cache 过期时间要合适,太长就长期有效,太短你看不到测试结果。建议 5-20秒。
最后直接在需要添加缓存的方法上使用注解就可实现缓存:
更多redis相关知识,请访问redis使用教程栏目!
以上就是spring中redis怎么用的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product