spring data redis使用示例
生活随笔
收集整理的這篇文章主要介紹了
spring data redis使用示例
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1. 配置依賴文件
<dependencies><dependency><groupId>org.springframework.data</groupId><artifactId>spring-data-redis</artifactId><version>1.5.0.RELEASE</version></dependency> </dependencies>2. 配置模板
<bean id="jedisConnFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" p:use-pool="true"/><!-- redis template definition --> <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate" p:connection-factory-ref="jedisConnFactory"/>3. 使用示例:
? ?3.1 K-V字符串類型的使用
? ? get方法:
? ??redisTemplate.opsForValue().get(key);
? ? set方法:
/*** @param key* @param value* @param liveTime*/private void set(String key, String value, long liveTime) {redisTemplate.opsForValue().set(key, value, liveTime, TimeUnit.SECONDS);}?3.2 list類型
public class Example {// inject the actual template @Autowiredprivate RedisTemplate<String, String> template;// inject the template as ListOperations// can also inject as Value, Set, ZSet, and HashOperations@Resource(name="redisTemplate")private ListOperations<String, String> listOps;public void addLink(String userId, URL url) {listOps.leftPush(userId, url.toExternalForm());// or use template directly redisTemplate.boundListOps(userId).leftPush(url.toExternalForm());} }類似的,其它類型可以使用
RedisTemplate的opsForX()方法
?
參考文獻:
http://projects.spring.io/spring-data-redis/
?
轉載于:https://www.cnblogs.com/davidwang456/p/4554887.html
《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀總結
以上是生活随笔為你收集整理的spring data redis使用示例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: jackson使用示例
- 下一篇: spring jdbctemplate源