共計(jì) 3236 個(gè)字符,預(yù)計(jì)需要花費(fèi) 9 分鐘才能閱讀完成。
丸趣 TV 小編給大家分享一下 spring 如何整合 redis 使用,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
1. 簡(jiǎn)單介紹
redis 是基于 C 語(yǔ)言開(kāi)發(fā)。
redis 是一個(gè) key-value 存儲(chǔ)系統(tǒng)。和 Memcached 類似,它支持存儲(chǔ)的 value 類型相對(duì)更多,包括 string(字符串)、list(鏈表)、set(集合)、zset(sorted set – 有序集合) 和 hash(哈希類型)。
redis 是一個(gè) 緩存數(shù)據(jù)庫(kù) (片面的理解) 既可以做緩存,也可以將數(shù)據(jù)持久化到磁盤中!
2.pom.xml 引入相關(guān) jar(曾經(jīng)因 jar 版本問(wèn)題出現(xiàn)報(bào)錯(cuò),請(qǐng)注意)
dependency
groupId org.apache.commons /groupId
artifactId commons-pool2 /artifactId
version 2.2 /version
/dependency
groupId org.springframework.data /groupId
artifactId spring-data-redis /artifactId
version 1.7.5.RELEASE /version
/dependency
dependency
groupId redis.clients /groupId
artifactId jedis /artifactId
version 2.9.0 /version
/dependency
3.spring-redis.xml 配置文件,配置關(guān)鍵 bean redisTemplate
?xml version= 1.0 encoding= UTF-8 ?
beans xmlns= http://www.springframework.org/schema/beans
xmlns:context= http://www.springframework.org/schema/context xmlns:p= http://www.springframework.org/schema/p
xmlns:mvc= http://www.springframework.org/schema/mvc xmlns:xsi= http://www.w3.org/2001/XMLSchema-instance
xmlns:tx= http://www.springframework.org/schema/tx xmlns:util= http://www.springframework.org/schema/util
xmlns:aop= http://www.springframework.org/schema/aop
xsi:schemaLocation= http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.0.xsd
!-- context:property-placeholder location= classpath:redis-config.properties /
--
bean id= jedisPoolConfig > 上文中使用到的配置文件 redis-config.properteis
redis.maxIdle=1
redis.maxTotal=5
redis.maxWaitMillis=30000
redis.testOnBorrow=true
redis.hostname=127.0.0.1
redis.port=6379
4.redis 有 4 個(gè)關(guān)鍵的接口如下
private ValueOperations K, V valueOps;
private ListOperations K, V listOps;
private SetOperations K, V setOps;
private ZSetOperations K, V zSetOps;
分別對(duì)應(yīng) redis 的數(shù)據(jù)類型:string(字符串),hash(哈希),list(列表),set(集合)及 zset(sorted set:有序集合)
具體使用如下,上代碼:
// 添加字符串
ValueOperations String, String value = this.redisTemplate.opsForValue();
value.set( hello , 討厭
System.out.println(value.get( hello));
// 添加 一個(gè) hash 集合
HashOperations String, Object, Object hash =redisTemplate.opsForHash();
hash.put( 沃爾瑪 , 水果 , 蘋果
hash.put( 沃爾瑪 , 飲料 , 紅牛
System.out.println(hash.entries( 沃爾瑪));
// 添加一個(gè) list 集合
ListOperations String, Object list = redisTemplate.opsForList();
list.rightPush( 課程 , chinese
list.rightPush( 課程 , englise
System.out.println(list.range( lpList , 0, 1));
// 添加 一個(gè) set 集合
SetOperations String, Object set = redisTemplate.opsForSet();
set.add( lpSet , lp
set.add( lpSet , 26
set.add( lpSet , 178cm
// 輸出 set 集合
System.out.println(set.members( lpSet));
// 添加有序的 set 集合
ZSetOperations String, Object zset = redisTemplate.opsForZSet();
zset.add(lpZset , lp , 0);
zset.add(lpZset , 26 , 2);
zset.add(lpZset , 178cm , 1);
// 輸出有序 set 集合
System.out.println(zset.rangeByScore( lpZset , 0, 2));
以上是“spring 如何整合 redis 使用”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注丸趣 TV 行業(yè)資訊頻道!