IDEA SSM整合Redis項(xiàng)目實(shí)例 附源碼
IDEA SSM整合Redis項(xiàng)目實(shí)例
1、pom.xml 配置
<!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
2、spring-redis.xml 配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- Redis連接池的設(shè)置 -->
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<!-- 控制一個(gè)pool可分配多少個(gè)jedis實(shí)例 -->
<property name="maxTotal" value="${redis.pool.maxActive}" />
<!-- 連接池中最多可空閑maxIdle個(gè)連接 ,這里取值為20,表示即使沒有數(shù)據(jù)庫(kù)連接時(shí)依然可以保持20空閑的連接,而不被清除,隨時(shí)處于待命狀態(tài)。 -->
<property name="maxIdle" value="${redis.pool.maxIdle}" />
<!-- 最大等待時(shí)間:當(dāng)沒有可用連接時(shí),連接池等待連接被歸還的最大時(shí)間(以毫秒計(jì)數(shù)),超過時(shí)間則拋出異常 -->
<property name="maxWaitMillis" value="${redis.pool.maxWait}" />
<!-- 在獲取連接的時(shí)候檢查有效性 -->
<property name="testOnBorrow" value="${redis.pool.testOnBorrow}" />
</bean>
<!-- 創(chuàng)建Redis連接池,并做相關(guān)配置 -->
<bean id="jedisWritePool" class="com.mlr.controller.cache.JedisPoolWriper"
depends-on="jedisPoolConfig">
<constructor-arg index="0" ref="jedisPoolConfig" />
<constructor-arg index="1" value="${redis.hostname}" />
<constructor-arg index="2" value="${redis.port}" type="int" />
</bean>
<!-- 創(chuàng)建Redis工具類,封裝好Redis的連接以進(jìn)行相關(guān)的操作 -->
<bean id="jedisUtil" class="com.mlr.controller.cache.JedisUtil" scope="singleton">
<property name="jedisPool">
<ref bean="jedisWritePool" />
</property>
</bean>
<!-- Redis的key操作 -->
<bean id="jedisKeys" class="com.mlr.controller.cache.JedisUtil$Keys"
scope="singleton">
<constructor-arg ref="jedisUtil"></constructor-arg>
</bean>
<!-- Redis的Strings操作 -->
<bean id="jedisStrings" class="com.mlr.controller.cache.JedisUtil$Strings"
scope="singleton">
<constructor-arg ref="jedisUtil"></constructor-arg>
</bean>
<!-- Redis的Lists操作 -->
<bean id="jedisLists" class="com.mlr.controller.cache.JedisUtil$Lists"
scope="singleton">
<constructor-arg ref="jedisUtil"></constructor-arg>
</bean>
<!-- Redis的Sets操作 -->
<bean id="jedisSets" class="com.mlr.controller.cache.JedisUtil$Sets"
scope="singleton">
<constructor-arg ref="jedisUtil"></constructor-arg>
</bean>
<!-- Redis的HashMap操作 -->
<bean id="jedisHash" class="com.mlr.controller.cache.JedisUtil$Hash"
scope="singleton">
<constructor-arg ref="jedisUtil"></constructor-arg>
</bean>
</beans>
3、redis.properties 配置
redis.hostname=Redis所在服務(wù)器ip redis.port=6379 redis.database=0 redis.pool.maxActive=100 redis.pool.maxIdle=20 redis.pool.maxWait=3000 redis.pool.testOnBorrow=true
4、redis連接池和工具類 JedisUtil.java
package com.mlr.controller.cache;
import com.mlr.controller.cache.JedisPoolWriper;
import java.util.List;
import java.util.Map;
import java.util.Set;
import redis.clients.jedis.BinaryClient.LIST_POSITION;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.SortingParams;
import redis.clients.util.SafeEncoder;
public class JedisUtil {
/**
* 緩存生存時(shí)間
*/
private final int expire = 60000;
/**
* 操作Key的方法
*/
public Keys KEYS;
/**
* 對(duì)存儲(chǔ)結(jié)構(gòu)為String類型的操作
*/
public Strings STRINGS;
/**
* 對(duì)存儲(chǔ)結(jié)構(gòu)為L(zhǎng)ist類型的操作
*/
public Lists LISTS;
/**
* 對(duì)存儲(chǔ)結(jié)構(gòu)為Set類型的操作
*/
public Sets SETS;
/**
* 對(duì)存儲(chǔ)結(jié)構(gòu)為HashMap類型的操作
*/
public Hash HASH;
/**
* Redis連接池對(duì)象
*/
private JedisPool jedisPool;
/**
* 獲取redis連接池
*/
public JedisPool getJedisPool() {
return jedisPool;
}
/**
* 設(shè)置redis連接池
*/
public void setJedisPool(JedisPoolWriper jedisPoolWriper) {
this.jedisPool = jedisPoolWriper.getJedisPool();
}
/**
* 從jedis連接池中獲取獲取jedis對(duì)象
*/
public Jedis getJedis() {
return jedisPool.getResource();
}
/**
* 設(shè)置過期時(shí)間
*
* @author xiangze
*/
public void expire(String key, int seconds) {
if (seconds <= 0) {
return;
}
Jedis jedis = getJedis();
jedis.expire(key, seconds);
jedis.close();
}
/**
* 設(shè)置默認(rèn)過期時(shí)間
*
* @author xiangze
*/
public void expire(String key) {
expire(key, expire);
}
// *******************************************Keys*******************************************//
public class Keys {
public Keys(JedisUtil jedisUtil) {
}
/**
* 清空所有key
*/
public String flushAll() {
Jedis jedis = getJedis();
String stata = jedis.flushAll();
jedis.close();
return stata;
}
/**
* 更改key
*
* @return 狀態(tài)碼
*/
public String rename(String oldkey, String newkey) {
return rename(SafeEncoder.encode(oldkey), SafeEncoder.encode(newkey));
}
/**
* 更改key,僅當(dāng)新key不存在時(shí)才執(zhí)行
*
* @return 狀態(tài)碼
*/
public long renamenx(String oldkey, String newkey) {
Jedis jedis = getJedis();
long status = jedis.renamenx(oldkey, newkey);
jedis.close();
return status;
}
/**
* 更改key
*
* @return 狀態(tài)碼
*/
public String rename(byte[] oldkey, byte[] newkey) {
Jedis jedis = getJedis();
String status = jedis.rename(oldkey, newkey);
jedis.close();
return status;
}
/**
* 設(shè)置key的過期時(shí)間,以秒為單位
* *
*/
public long expired(String key, int seconds) {
Jedis jedis = getJedis();
long count = jedis.expire(key, seconds);
jedis.close();
return count;
}
/**
* 設(shè)置key的過期時(shí)間,它是距歷元(即格林威治標(biāo)準(zhǔn)時(shí)間 1970 年 1 月 1 日的 00:00:00,格里高利歷)的偏移量。
*
* *
*/
public long expireAt(String key, long timestamp) {
Jedis jedis = getJedis();
long count = jedis.expireAt(key, timestamp);
jedis.close();
return count;
}
/**
* 查詢key的過期時(shí)間
*
* @param key
* @return 以秒為單位的時(shí)間表示
*/
public long ttl(String key) {
// ShardedJedis sjedis = getShardedJedis();
Jedis sjedis = getJedis();
long len = sjedis.ttl(key);
sjedis.close();
return len;
}
/**
* 取消對(duì)key過期時(shí)間的設(shè)置
*
* @return 影響的記錄數(shù)
*/
public long persist(String key) {
Jedis jedis = getJedis();
long count = jedis.persist(key);
jedis.close();
return count;
}
/**
* 刪除keys對(duì)應(yīng)的記錄,可以是多個(gè)key
*
* @param keys
* @return 刪除的記錄數(shù)
*/
public long del(String... keys) {
Jedis jedis = getJedis();
long count = jedis.del(keys);
jedis.close();
return count;
}
/**
* 刪除keys對(duì)應(yīng)的記錄,可以是多個(gè)key
*
* @param keys
* @return 刪除的記錄數(shù)
*/
public long del(byte[]... keys) {
Jedis jedis = getJedis();
long count = jedis.del(keys);
jedis.close();
return count;
}
/**
* 判斷key是否存在
*
* @param key
* @return boolean
*/
public boolean exists(String key) {
// ShardedJedis sjedis = getShardedJedis();
Jedis sjedis = getJedis();
boolean exis = sjedis.exists(key);
sjedis.close();
return exis;
}
/**
* 對(duì)List,Set,SortSet進(jìn)行排序,如果集合數(shù)據(jù)較大應(yīng)避免使用這個(gè)方法
*
* @param key
* @return List<String> 集合的全部記錄
**/
public List<String> sort(String key) {
// ShardedJedis sjedis = getShardedJedis();
Jedis sjedis = getJedis();
List<String> list = sjedis.sort(key);
sjedis.close();
return list;
}
/**
* 對(duì)List,Set,SortSet進(jìn)行排序或limit
*
* @param key
* @param parame 定義排序類型或limit的起止位置.
* @return List<String> 全部或部分記錄
**/
public List<String> sort(String key, SortingParams parame) {
// ShardedJedis sjedis = getShardedJedis();
Jedis sjedis = getJedis();
List<String> list = sjedis.sort(key, parame);
sjedis.close();
return list;
}
/**
* 返回指定key存儲(chǔ)的類型
*
* @param key
* @return String string|list|set|zset|hash
**/
public String type(String key) {
// ShardedJedis sjedis = getShardedJedis();
Jedis sjedis = getJedis();
String type = sjedis.type(key);
sjedis.close();
return type;
}
/**
* 查找所有匹配給定的模式的鍵
*
* @param pattern key的表達(dá)式,*表示多個(gè),?表示一個(gè)
*/
public Set<String> keys(String pattern) {
Jedis jedis = getJedis();
Set<String> set = jedis.keys(pattern);
jedis.close();
return set;
}
}
// *******************************************Strings*******************************************//
public class Strings {
public Strings(JedisUtil jedisUtil) {
}
/**
* 根據(jù)key獲取記錄
*
* @param key
* @return 值
*/
public String get(String key) {
// ShardedJedis sjedis = getShardedJedis();
Jedis sjedis = getJedis();
String value = sjedis.get(key);
sjedis.close();
return value;
}
/**
* 根據(jù)key獲取記錄
*
* @param key
* @return 值
*/
public byte[] get(byte[] key) {
// ShardedJedis sjedis = getShardedJedis();
Jedis sjedis = getJedis();
byte[] value = sjedis.get(key);
sjedis.close();
return value;
}
/**
* 添加記錄,如果記錄已存在將覆蓋原有的value
*
* @param key
* @param value
* @return 狀態(tài)碼
*/
public String set(String key, String value) {
return set(SafeEncoder.encode(key), SafeEncoder.encode(value));
}
/**
* 添加記錄,如果記錄已存在將覆蓋原有的value
*
* @param key
* @param value
* @return 狀態(tài)碼
*/
public String set(String key, byte[] value) {
return set(SafeEncoder.encode(key), value);
}
/**
* 添加記錄,如果記錄已存在將覆蓋原有的value
*
* @param key
* @param value
* @return 狀態(tài)碼
*/
public String set(byte[] key, byte[] value) {
Jedis jedis = getJedis();
String status = jedis.set(key, value);
jedis.close();
return status;
}
/**
* 添加有過期時(shí)間的記錄
*
* @param key
* @param seconds 過期時(shí)間,以秒為單位
* @param value
* @return String 操作狀態(tài)
*/
public String setEx(String key, int seconds, String value) {
Jedis jedis = getJedis();
String str = jedis.setex(key, seconds, value);
jedis.close();
return str;
}
/**
* 添加有過期時(shí)間的記錄
*
* @param key
* @param seconds 過期時(shí)間,以秒為單位
* @param value
* @return String 操作狀態(tài)
*/
public String setEx(byte[] key, int seconds, byte[] value) {
Jedis jedis = getJedis();
String str = jedis.setex(key, seconds, value);
jedis.close();
return str;
}
/**
* 添加一條記錄,僅當(dāng)給定的key不存在時(shí)才插入
*
* @param key
* @param value
* @return long 狀態(tài)碼,1插入成功且key不存在,0未插入,key存在
*/
public long setnx(String key, String value) {
Jedis jedis = getJedis();
long str = jedis.setnx(key, value);
jedis.close();
return str;
}
/**
* 從指定位置開始插入數(shù)據(jù),插入的數(shù)據(jù)會(huì)覆蓋指定位置以后的數(shù)據(jù)<br/>
* 例:String str1="123456789";<br/>
* 對(duì)str1操作后setRange(key,4,0000),str1="123400009";
*
* @param key
* @param offset
* @param value
* @return long value的長(zhǎng)度
*/
public long setRange(String key, long offset, String value) {
Jedis jedis = getJedis();
long len = jedis.setrange(key, offset, value);
jedis.close();
return len;
}
/**
* 在指定的key中追加value
*
* @param key
* @param value
* @return long 追加后value的長(zhǎng)度
**/
public long append(String key, String value) {
Jedis jedis = getJedis();
long len = jedis.append(key, value);
jedis.close();
return len;
}
/**
* 將key對(duì)應(yīng)的value減去指定的值,只有value可以轉(zhuǎn)為數(shù)字時(shí)該方法才可用
*
* @param key
* @param number 要減去的值
* @return long 減指定值后的值
*/
public long decrBy(String key, long number) {
Jedis jedis = getJedis();
long len = jedis.decrBy(key, number);
jedis.close();
return len;
}
/**
* <b>可以作為獲取唯一id的方法</b><br/>
* 將key對(duì)應(yīng)的value加上指定的值,只有value可以轉(zhuǎn)為數(shù)字時(shí)該方法才可用
*
* @param key
* @param number 要減去的值
* @return long 相加后的值
*/
public long incrBy(String key, long number) {
Jedis jedis = getJedis();
long len = jedis.incrBy(key, number);
jedis.close();
return len;
}
/**
* 對(duì)指定key對(duì)應(yīng)的value進(jìn)行截取
*
* @param key
* @param startOffset 開始位置(包含)
* @param endOffset 結(jié)束位置(包含)
* @return String 截取的值
*/
public String getrange(String key, long startOffset, long endOffset) {
// ShardedJedis sjedis = getShardedJedis();
Jedis sjedis = getJedis();
String value = sjedis.getrange(key, startOffset, endOffset);
sjedis.close();
return value;
}
/**
* 獲取并設(shè)置指定key對(duì)應(yīng)的value<br/>
* 如果key存在返回之前的value,否則返回null
*
* @param key
* @param value
* @return String 原始value或null
*/
public String getSet(String key, String value) {
Jedis jedis = getJedis();
String str = jedis.getSet(key, value);
jedis.close();
return str;
}
/**
* 批量獲取記錄,如果指定的key不存在返回List的對(duì)應(yīng)位置將是null
*
* @param keys
* @return List<String> 值得集合
*/
public List<String> mget(String... keys) {
Jedis jedis = getJedis();
List<String> str = jedis.mget(keys);
jedis.close();
return str;
}
/**
* 批量存儲(chǔ)記錄
*
* @param keysvalues 例:keysvalues="key1","value1","key2","value2";
* @return String 狀態(tài)碼
*/
public String mset(String... keysvalues) {
Jedis jedis = getJedis();
String str = jedis.mset(keysvalues);
jedis.close();
return str;
}
/**
* 獲取key對(duì)應(yīng)的值的長(zhǎng)度
*
* @param key
* @return value值得長(zhǎng)度
*/
public long strlen(String key) {
Jedis jedis = getJedis();
long len = jedis.strlen(key);
jedis.close();
return len;
}
}
// *******************************************Sets*******************************************//
public class Sets {
public Sets(JedisUtil jedisUtil) {
}
/**
* 向Set添加一條記錄,如果member已存在返回0,否則返回1
*
* @param key
* @param member
* @return 操作碼, 0或1
*/
public long sadd(String key, String member) {
Jedis jedis = getJedis();
long s = jedis.sadd(key, member);
jedis.close();
return s;
}
public long sadd(byte[] key, byte[] member) {
Jedis jedis = getJedis();
long s = jedis.sadd(key, member);
jedis.close();
return s;
}
/**
* 獲取給定key中元素個(gè)數(shù)
*
* @param key
* @return 元素個(gè)數(shù)
*/
public long scard(String key) {
// ShardedJedis sjedis = getShardedJedis();
Jedis sjedis = getJedis();
long len = sjedis.scard(key);
sjedis.close();
return len;
}
/**
* 返回從第一組和所有的給定集合之間的差異的成員
*
* @param keys
* @return 差異的成員集合
*/
public Set<String> sdiff(String... keys) {
Jedis jedis = getJedis();
Set<String> set = jedis.sdiff(keys);
jedis.close();
return set;
}
/**
* 這個(gè)命令等于sdiff,但返回的不是結(jié)果集,而是將結(jié)果集存儲(chǔ)在新的集合中,如果目標(biāo)已存在,則覆蓋。
*
* @param newkey 新結(jié)果集的key
* @param keys 比較的集合
* @return 新集合中的記錄數(shù)
**/
public long sdiffstore(String newkey, String... keys) {
Jedis jedis = getJedis();
long s = jedis.sdiffstore(newkey, keys);
jedis.close();
return s;
}
/**
* 返回給定集合交集的成員,如果其中一個(gè)集合為不存在或?yàn)榭眨瑒t返回空Set
*
* @param keys
* @return 交集成員的集合
**/
public Set<String> sinter(String... keys) {
Jedis jedis = getJedis();
Set<String> set = jedis.sinter(keys);
jedis.close();
return set;
}
/**
* 這個(gè)命令等于sinter,但返回的不是結(jié)果集,而是將結(jié)果集存儲(chǔ)在新的集合中,如果目標(biāo)已存在,則覆蓋。
*
* @param newkey 新結(jié)果集的key
* @param keys 比較的集合
* @return 新集合中的記錄數(shù)
**/
public long sinterstore(String newkey, String... keys) {
Jedis jedis = getJedis();
long s = jedis.sinterstore(newkey, keys);
jedis.close();
return s;
}
/**
* 確定一個(gè)給定的值是否存在
*
* @param key
* @param member 要判斷的值
* @return 存在返回1,不存在返回0
**/
public boolean sismember(String key, String member) {
// ShardedJedis sjedis = getShardedJedis();
Jedis sjedis = getJedis();
boolean s = sjedis.sismember(key, member);
sjedis.close();
return s;
}
/**
* 返回集合中的所有成員
*
* @param key
* @return 成員集合
*/
public Set<String> smembers(String key) {
// ShardedJedis sjedis = getShardedJedis();
Jedis sjedis = getJedis();
Set<String> set = sjedis.smembers(key);
sjedis.close();
return set;
}
public Set<byte[]> smembers(byte[] key) {
// ShardedJedis sjedis = getShardedJedis();
Jedis sjedis = getJedis();
Set<byte[]> set = sjedis.smembers(key);
sjedis.close();
return set;
}
/**
* 將成員從源集合移出放入目標(biāo)集合 <br/>
* 如果源集合不存在或不包哈指定成員,不進(jìn)行任何操作,返回0<br/>
* 否則該成員從源集合上刪除,并添加到目標(biāo)集合,如果目標(biāo)集合中成員已存在,則只在源集合進(jìn)行刪除
*
* @param srckey 源集合
* @param dstkey 目標(biāo)集合
* @param member 源集合中的成員
* @return 狀態(tài)碼,1成功,0失敗
*/
public long smove(String srckey, String dstkey, String member) {
Jedis jedis = getJedis();
long s = jedis.smove(srckey, dstkey, member);
jedis.close();
return s;
}
/**
* 從集合中刪除成員
*
* @param key
* @return 被刪除的成員
*/
public String spop(String key) {
Jedis jedis = getJedis();
String s = jedis.spop(key);
jedis.close();
return s;
}
/**
* 從集合中刪除指定成員
*
* @param key
* @param member 要?jiǎng)h除的成員
* @return 狀態(tài)碼,成功返回1,成員不存在返回0
*/
public long srem(String key, String member) {
Jedis jedis = getJedis();
long s = jedis.srem(key, member);
jedis.close();
return s;
}
/**
* 合并多個(gè)集合并返回合并后的結(jié)果,合并后的結(jié)果集合并不保存<br/>
*
* @param keys
* @return 合并后的結(jié)果集合
* @see
*/
public Set<String> sunion(String... keys) {
Jedis jedis = getJedis();
Set<String> set = jedis.sunion(keys);
jedis.close();
return set;
}
/**
* 合并多個(gè)集合并將合并后的結(jié)果集保存在指定的新集合中,如果新集合已經(jīng)存在則覆蓋
*
* @param newkey 新集合的key
* @param keys 要合并的集合
**/
public long sunionstore(String newkey, String... keys) {
Jedis jedis = getJedis();
long s = jedis.sunionstore(newkey, keys);
jedis.close();
return s;
}
}
// *******************************************Hash*******************************************//
public class Hash {
public Hash(JedisUtil jedisUtil) {
}
/**
* 從hash中刪除指定的存儲(chǔ)
*
* @param key
* @param fieid 存儲(chǔ)的名字
* @return 狀態(tài)碼,1成功,0失敗
*/
public long hdel(String key, String fieid) {
Jedis jedis = getJedis();
long s = jedis.hdel(key, fieid);
jedis.close();
return s;
}
public long hdel(String key) {
Jedis jedis = getJedis();
long s = jedis.del(key);
jedis.close();
return s;
}
/**
* 測(cè)試hash中指定的存儲(chǔ)是否存在
*
* @param key
* @param fieid 存儲(chǔ)的名字
* @return 1存在,0不存在
*/
public boolean hexists(String key, String fieid) {
// ShardedJedis sjedis = getShardedJedis();
Jedis sjedis = getJedis();
boolean s = sjedis.hexists(key, fieid);
sjedis.close();
return s;
}
/**
* 返回hash中指定存儲(chǔ)位置的值
*
* @param key
* @param fieid 存儲(chǔ)的名字
* @return 存儲(chǔ)對(duì)應(yīng)的值
*/
public String hget(String key, String fieid) {
// ShardedJedis sjedis = getShardedJedis();
Jedis sjedis = getJedis();
String s = sjedis.hget(key, fieid);
sjedis.close();
return s;
}
public byte[] hget(byte[] key, byte[] fieid) {
// ShardedJedis sjedis = getShardedJedis();
Jedis sjedis = getJedis();
byte[] s = sjedis.hget(key, fieid);
sjedis.close();
return s;
}
/**
* 以Map的形式返回hash中的存儲(chǔ)和值
*
* @param key
* @return Map<Strinig,String>
*/
public Map<String, String> hgetAll(String key) {
// ShardedJedis sjedis = getShardedJedis();
Jedis sjedis = getJedis();
Map<String, String> map = sjedis.hgetAll(key);
sjedis.close();
return map;
}
/**
* 添加一個(gè)對(duì)應(yīng)關(guān)系
*
* @param key
* @param fieid
* @param value
* @return 狀態(tài)碼 1成功,0失敗,fieid已存在將更新,也返回0
**/
public long hset(String key, String fieid, String value) {
Jedis jedis = getJedis();
long s = jedis.hset(key, fieid, value);
jedis.close();
return s;
}
public long hset(String key, String fieid, byte[] value) {
Jedis jedis = getJedis();
long s = jedis.hset(key.getBytes(), fieid.getBytes(), value);
jedis.close();
return s;
}
/**
* 添加對(duì)應(yīng)關(guān)系,只有在fieid不存在時(shí)才執(zhí)行
*
* @param key
* @param fieid
* @param value
* @return 狀態(tài)碼 1成功,0失敗fieid已存
**/
public long hsetnx(String key, String fieid, String value) {
Jedis jedis = getJedis();
long s = jedis.hsetnx(key, fieid, value);
jedis.close();
return s;
}
/**
* 獲取hash中value的集合
*
* @param key
* @return List<String>
*/
public List<String> hvals(String key) {
// ShardedJedis sjedis = getShardedJedis();
Jedis sjedis = getJedis();
List<String> list = sjedis.hvals(key);
sjedis.close();
return list;
}
/**
* 在指定的存儲(chǔ)位置加上指定的數(shù)字,存儲(chǔ)位置的值必須可轉(zhuǎn)為數(shù)字類型
*
* @param key
* @param fieid 存儲(chǔ)位置
* @param value 要增加的值,可以是負(fù)數(shù)
* @return 增加指定數(shù)字后,存儲(chǔ)位置的值
*/
public long hincrby(String key, String fieid, long value) {
Jedis jedis = getJedis();
long s = jedis.hincrBy(key, fieid, value);
jedis.close();
return s;
}
/**
* 返回指定hash中的所有存儲(chǔ)名字,類似Map中的keySet方法
*
* @param key
* @return Set<String> 存儲(chǔ)名稱的集合
*/
public Set<String> hkeys(String key) {
// ShardedJedis sjedis = getShardedJedis();
Jedis sjedis = getJedis();
Set<String> set = sjedis.hkeys(key);
sjedis.close();
return set;
}
/**
* 獲取hash中存儲(chǔ)的個(gè)數(shù),類似Map中size方法
*
* @param key
* @return long 存儲(chǔ)的個(gè)數(shù)
*/
public long hlen(String key) {
// ShardedJedis sjedis = getShardedJedis();
Jedis sjedis = getJedis();
long len = sjedis.hlen(key);
sjedis.close();
return len;
}
/**
* 根據(jù)多個(gè)key,獲取對(duì)應(yīng)的value,返回List,如果指定的key不存在,List對(duì)應(yīng)位置為null
*
* @param key
* @param fieids 存儲(chǔ)位置
* @return List<String>
*/
public List<String> hmget(String key, String... fieids) {
// ShardedJedis sjedis = getShardedJedis();
Jedis sjedis = getJedis();
List<String> list = sjedis.hmget(key, fieids);
sjedis.close();
return list;
}
public List<byte[]> hmget(byte[] key, byte[]... fieids) {
// ShardedJedis sjedis = getShardedJedis();
Jedis sjedis = getJedis();
List<byte[]> list = sjedis.hmget(key, fieids);
sjedis.close();
return list;
}
/**
* 添加對(duì)應(yīng)關(guān)系,如果對(duì)應(yīng)關(guān)系已存在,則覆蓋
*
* @param key
* @param map 對(duì)應(yīng)關(guān)系
* @return 狀態(tài),成功返回OK
*/
public String hmset(String key, Map<String, String> map) {
Jedis jedis = getJedis();
String s = jedis.hmset(key, map);
jedis.close();
return s;
}
/**
* 添加對(duì)應(yīng)關(guān)系,如果對(duì)應(yīng)關(guān)系已存在,則覆蓋
*
* @param key
* @param map 對(duì)應(yīng)關(guān)系
* @return 狀態(tài),成功返回OK
*/
public String hmset(byte[] key, Map<byte[], byte[]> map) {
Jedis jedis = getJedis();
String s = jedis.hmset(key, map);
jedis.close();
return s;
}
}
// *******************************************Lists*******************************************//
public class Lists {
public Lists(JedisUtil jedisUtil) {
}
/**
* List長(zhǎng)度
*
* @param key
* @return 長(zhǎng)度
*/
public long llen(String key) {
return llen(SafeEncoder.encode(key));
}
/**
* List長(zhǎng)度
*
* @param key
* @return 長(zhǎng)度
*/
public long llen(byte[] key) {
// ShardedJedis sjedis = getShardedJedis();
Jedis sjedis = getJedis();
long count = sjedis.llen(key);
sjedis.close();
return count;
}
/**
* 覆蓋操作,將覆蓋List中指定位置的值
*
* @param key
* @param index 位置
* @param value 值
* @return 狀態(tài)碼
*/
public String lset(byte[] key, int index, byte[] value) {
Jedis jedis = getJedis();
String status = jedis.lset(key, index, value);
jedis.close();
return status;
}
/**
* 覆蓋操作,將覆蓋List中指定位置的值
*
* @param index 位置
* @param value 值
* @return 狀態(tài)碼
*/
public String lset(String key, int index, String value) {
return lset(SafeEncoder.encode(key), index, SafeEncoder.encode(value));
}
/**
* 在value的相對(duì)位置插入記錄
*
* @param where 前面插入或后面插入
* @param pivot 相對(duì)位置的內(nèi)容
* @param value 插入的內(nèi)容
* @return 記錄總數(shù)
*/
public long linsert(String key, LIST_POSITION where, String pivot, String value) {
return linsert(SafeEncoder.encode(key), where, SafeEncoder.encode(pivot),
SafeEncoder.encode(value));
}
/**
* 在指定位置插入記錄
*
* @param key
* @param where 前面插入或后面插入
* @param pivot 相對(duì)位置的內(nèi)容
* @param value 插入的內(nèi)容
* @return 記錄總數(shù)
*/
public long linsert(byte[] key, LIST_POSITION where, byte[] pivot, byte[] value) {
Jedis jedis = getJedis();
long count = jedis.linsert(key, where, pivot, value);
jedis.close();
return count;
}
/**
* 獲取List中指定位置的值
*
* @param key
* @param index 位置
* @return 值
**/
public String lindex(String key, int index) {
return SafeEncoder.encode(lindex(SafeEncoder.encode(key), index));
}
/**
* 獲取List中指定位置的值
*
* @param key
* @param index 位置
* @return 值
**/
public byte[] lindex(byte[] key, int index) {
// ShardedJedis sjedis = getShardedJedis();
Jedis sjedis = getJedis();
byte[] value = sjedis.lindex(key, index);
sjedis.close();
return value;
}
/**
* 將List中的第一條記錄移出List
*
* @param key
* @return 移出的記錄
*/
public String lpop(String key) {
return SafeEncoder.encode(lpop(SafeEncoder.encode(key)));
}
/**
* 將List中的第一條記錄移出List
*
* @param key
* @return 移出的記錄
*/
public byte[] lpop(byte[] key) {
Jedis jedis = getJedis();
byte[] value = jedis.lpop(key);
jedis.close();
return value;
}
/**
* 將List中最后第一條記錄移出List
*
* @param key
* @return 移出的記錄
*/
public String rpop(String key) {
Jedis jedis = getJedis();
String value = jedis.rpop(key);
jedis.close();
return value;
}
/**
* 向List尾部追加記錄
*
* @param key
* @param value
* @return 記錄總數(shù)
*/
public long lpush(String key, String value) {
return lpush(SafeEncoder.encode(key), SafeEncoder.encode(value));
}
/**
* 向List頭部追加記錄
*
* @param key
* @param value
* @return 記錄總數(shù)
*/
public long rpush(String key, String value) {
Jedis jedis = getJedis();
long count = jedis.rpush(key, value);
jedis.close();
return count;
}
/**
* 向List頭部追加記錄
*
* @param key
* @param value
* @return 記錄總數(shù)
*/
public long rpush(byte[] key, byte[] value) {
Jedis jedis = getJedis();
long count = jedis.rpush(key, value);
jedis.close();
return count;
}
/**
* 向List中追加記錄
*
* @param key
* @param value
* @return 記錄總數(shù)
*/
public long lpush(byte[] key, byte[] value) {
Jedis jedis = getJedis();
long count = jedis.lpush(key, value);
jedis.close();
return count;
}
/**
* 獲取指定范圍的記錄,可以做為分頁使用
*
* @param key
* @param start
* @param end
* @return List
*/
public List<String> lrange(String key, long start, long end) {
// ShardedJedis sjedis = getShardedJedis();
Jedis sjedis = getJedis();
List<String> list = sjedis.lrange(key, start, end);
sjedis.close();
return list;
}
/**
* 獲取指定范圍的記錄,可以做為分頁使用
*
* @param key
* @param start
* @param end 如果為負(fù)數(shù),則尾部開始計(jì)算
* @return List
*/
public List<byte[]> lrange(byte[] key, int start, int end) {
// ShardedJedis sjedis = getShardedJedis();
Jedis sjedis = getJedis();
List<byte[]> list = sjedis.lrange(key, start, end);
sjedis.close();
return list;
}
/**
* 刪除List中c條記錄,被刪除的記錄值為value
*
* @param key
* @param c 要?jiǎng)h除的數(shù)量,如果為負(fù)數(shù)則從List的尾部檢查并刪除符合的記錄
* @param value 要匹配的值
* @return 刪除后的List中的記錄數(shù)
*/
public long lrem(byte[] key, int c, byte[] value) {
Jedis jedis = getJedis();
long count = jedis.lrem(key, c, value);
jedis.close();
return count;
}
/**
* 刪除List中c條記錄,被刪除的記錄值為value
*
* @param key
* @param c 要?jiǎng)h除的數(shù)量,如果為負(fù)數(shù)則從List的尾部檢查并刪除符合的記錄
* @param value 要匹配的值
* @return 刪除后的List中的記錄數(shù)
*/
public long lrem(String key, int c, String value) {
return lrem(SafeEncoder.encode(key), c, SafeEncoder.encode(value));
}
/**
* 算是刪除吧,只保留start與end之間的記錄
*
* @param key
* @param start 記錄的開始位置(0表示第一條記錄)
* @param end 記錄的結(jié)束位置(如果為-1則表示最后一個(gè),-2,-3以此類推)
* @return 執(zhí)行狀態(tài)碼
*/
public String ltrim(byte[] key, int start, int end) {
Jedis jedis = getJedis();
String str = jedis.ltrim(key, start, end);
jedis.close();
return str;
}
/**
* 算是刪除吧,只保留start與end之間的記錄
*
* @param key
* @param start 記錄的開始位置(0表示第一條記錄)
* @param end 記錄的結(jié)束位置(如果為-1則表示最后一個(gè),-2,-3以此類推)
* @return 執(zhí)行狀態(tài)碼
*/
public String ltrim(String key, int start, int end) {
return ltrim(SafeEncoder.encode(key), start, end);
}
}
}
5、JedisPoolWriper.java
package com.mlr.controller.cache;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
/**
* 強(qiáng)指定redis的JedisPool接口構(gòu)造函數(shù),這樣才能在centos成功創(chuàng)建jedispool
*
* @author xiangze
*
*/
public class JedisPoolWriper {
/** Redis連接池對(duì)象 */
private JedisPool jedisPool;
public JedisPoolWriper(final JedisPoolConfig poolConfig, final String host,
final int port) {
try {
jedisPool = new JedisPool(poolConfig, host, port);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 獲取Redis連接池對(duì)象
* @return
*/
public JedisPool getJedisPool() {
return jedisPool;
}
/**
* 注入Redis連接池對(duì)象
* @param jedisPool
*/
public void setJedisPool(JedisPool jedisPool) {
this.jedisPool = jedisPool;
}
}
6、然后我們的Jedis就可以用了
把我們的業(yè)務(wù)邏輯中不需要頻繁更換的數(shù)據(jù)保存到redis中,例如,下部分代碼:
@Transactional
public List<Area> getAreaList(){// 定義redis的key
String key = AREALISTKEY;
// 定義接收對(duì)象
List<Area> areaList = null;
// 定義jackson數(shù)據(jù)轉(zhuǎn)換操作類
ObjectMapper mapper = new ObjectMapper();
// 判斷key是否存在
if (!jedisKeys.exists(key)) {
// 若不存在,則從數(shù)據(jù)庫(kù)里面取出相應(yīng)數(shù)據(jù)
areaList = areaDao.queryArea();
// 將相關(guān)的實(shí)體類集合轉(zhuǎn)換成string,存入redis里面對(duì)應(yīng)的key中
String jsonString;
try {
jsonString = mapper.writeValueAsString(areaList);
} catch (JsonProcessingException e) {
e.printStackTrace();
logger.error(e.getMessage());
throw new AreaOperationException(e.getMessage());
}
jedisStrings.set(key, jsonString);
} else {
// 若存在,則直接從redis里面取出相應(yīng)數(shù)據(jù)
String jsonString = jedisStrings.get(key);
// 指定要將string轉(zhuǎn)換成的集合類型
JavaType javaType = mapper.getTypeFactory().constructParametricType(ArrayList.class, Area.class);
try {
// 將相關(guān)key對(duì)應(yīng)的value里的的string轉(zhuǎn)換成對(duì)象的實(shí)體類集合
areaList = mapper.readValue(jsonString, javaType);
} catch (JsonParseException e) {
e.printStackTrace();
logger.error(e.getMessage());
throw new AreaOperationException(e.getMessage());
} catch (JsonMappingException e) {
e.printStackTrace();
logger.error(e.getMessage());
throw new AreaOperationException(e.getMessage());
} catch (IOException e) {
e.printStackTrace();
logger.error(e.getMessage());
throw new AreaOperationException(e.getMessage());
}
}
return areaList;
}
以上就是IDEA SSM整合Redis項(xiàng)目實(shí)例的詳細(xì)內(nèi)容,更多關(guān)于IDEA SSM整合Redis的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
SpringBoot項(xiàng)目中jar發(fā)布獲取jar包所在目錄路徑的最佳方法
在開發(fā)過程中,我們經(jīng)常要遇到上傳圖片、word、pdf等功能,但是當(dāng)我們把項(xiàng)目打包發(fā)布到服務(wù)器上時(shí),對(duì)應(yīng)的很多存儲(chǔ)路徑的方法就會(huì)失效,下面這篇文章主要給大家介紹了關(guān)于SpringBoot項(xiàng)目中jar發(fā)布獲取jar包所在目錄路徑的相關(guān)資料2022-07-07
spring事務(wù)隔離級(jí)別、傳播機(jī)制以及簡(jiǎn)單配置方式
這篇文章主要介紹了spring事務(wù)隔離級(jí)別、傳播機(jī)制以及簡(jiǎn)單配置方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-01-01
使用Spring AOP實(shí)現(xiàn)MySQL數(shù)據(jù)庫(kù)讀寫分離案例分析(附demo)
分布式環(huán)境下數(shù)據(jù)庫(kù)的讀寫分離策略是解決數(shù)據(jù)庫(kù)讀寫性能瓶頸的一個(gè)關(guān)鍵解決方案,這篇文章主要介紹了使用Spring AOP實(shí)現(xiàn)MySQL數(shù)據(jù)庫(kù)讀寫分離案例分析(附demo),有興趣的可以了解一下。2017-01-01
springboot中用fastjson處理返回值為null的屬性值
在本篇文章里小編給大家整理的是一篇關(guān)于springboot中用fastjson處理返回值問題詳解內(nèi)容,需要的朋友們參考下。2020-03-03
Java-Redis-Redisson分布式鎖的功能使用及實(shí)現(xiàn)
這篇文章主要介紹了Java-Redis-Redisson-分布式鎖的功能使用及實(shí)現(xiàn),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-08-08
Springboot hibernate envers使用過程詳解
這篇文章主要介紹了Springboot hibernate envers使用過程詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06

