溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

IDEA SSM整合Redis項(xiàng)目的示例分析

發(fā)布時間:2021-06-08 14:44:11 來源:億速云 閱讀:118 作者:小新 欄目:開發(fā)技術(shù)

這篇文章給大家分享的是有關(guān)IDEA SSM整合Redis項(xiàng)目的示例分析的內(nèi)容。小編覺得挺實(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">
		<!-- 控制一個pool可分配多少個jedis實(shí)例 -->
		<property name="maxTotal" value="${redis.pool.maxActive}" />
		<!-- 連接池中最多可空閑maxIdle個連接 ,這里取值為20,表示即使沒有數(shù)據(jù)庫連接時依然可以保持20空閑的連接,而不被清除,隨時處于待命狀態(tài)。 -->
		<property name="maxIdle" value="${redis.pool.maxIdle}" />
		<!-- 最大等待時間:當(dāng)沒有可用連接時,連接池等待連接被歸還的最大時間(以毫秒計(jì)數(shù)),超過時間則拋出異常 -->
		<property name="maxWaitMillis" value="${redis.pool.maxWait}" />
		<!-- 在獲取連接的時候檢查有效性 -->
		<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 {

  /**
   * 緩存生存時間
   */
  private final int expire = 60000;
  /**
   * 操作Key的方法
   */
  public Keys KEYS;
  /**
   * 對存儲結(jié)構(gòu)為String類型的操作
   */
  public Strings STRINGS;
  /**
   * 對存儲結(jié)構(gòu)為List類型的操作
   */
  public Lists LISTS;
  /**
   * 對存儲結(jié)構(gòu)為Set類型的操作
   */
  public Sets SETS;
  /**
   * 對存儲結(jié)構(gòu)為HashMap類型的操作
   */
  public Hash HASH;

  /**
   * Redis連接池對象
   */
  private JedisPool jedisPool;

  /**
   * 獲取redis連接池
   */
  public JedisPool getJedisPool() {
    return jedisPool;
  }

  /**
   * 設(shè)置redis連接池
   */
  public void setJedisPool(JedisPoolWriper jedisPoolWriper) {
    this.jedisPool = jedisPoolWriper.getJedisPool();
  }

  /**
   * 從jedis連接池中獲取獲取jedis對象
   */
  public Jedis getJedis() {
    return jedisPool.getResource();
  }

  /**
   * 設(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)過期時間
   *
   * @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不存在時才執(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的過期時間,以秒為單位
     * *
     */
    public long expired(String key, int seconds) {
      Jedis jedis = getJedis();
      long count = jedis.expire(key, seconds);
      jedis.close();
      return count;
    }

    /**
     * 設(shè)置key的過期時間,它是距歷元(即格林威治標(biāo)準(zhǔn)時間 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的過期時間
     *
     * @param  key
     * @return 以秒為單位的時間表示
     */
    public long ttl(String key) {
      // ShardedJedis sjedis = getShardedJedis();
      Jedis sjedis = getJedis();
      long len = sjedis.ttl(key);
      sjedis.close();
      return len;
    }

    /**
     * 取消對key過期時間的設(shè)置
     *
     * @return 影響的記錄數(shù)
     */
    public long persist(String key) {
      Jedis jedis = getJedis();
      long count = jedis.persist(key);
      jedis.close();
      return count;
    }

    /**
     * 刪除keys對應(yīng)的記錄,可以是多個key
     *
     * @param  keys
     * @return 刪除的記錄數(shù)
     */
    public long del(String... keys) {
      Jedis jedis = getJedis();
      long count = jedis.del(keys);
      jedis.close();
      return count;
    }

    /**
     * 刪除keys對應(yīng)的記錄,可以是多個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;
    }

    /**
     * 對List,Set,SortSet進(jìn)行排序,如果集合數(shù)據(jù)較大應(yīng)避免使用這個方法
     *
     * @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;
    }

    /**
     * 對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存儲的類型
     *
     * @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á)式,*表示多個,?表示一個
     */
    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;
    }

    /**
     * 添加有過期時間的記錄
     *
     * @param  key
     * @param  seconds 過期時間,以秒為單位
     * @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;
    }

    /**
     * 添加有過期時間的記錄
     *
     * @param  key
     * @param  seconds 過期時間,以秒為單位
     * @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不存在時才插入
     *
     * @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ù)會覆蓋指定位置以后的數(shù)據(jù)<br/>
     * 例:String str1="123456789";<br/>
     * 對str1操作后setRange(key,4,0000),str1="123400009";
     *
     * @param  key
     * @param  offset
     * @param  value
     * @return long value的長度
     */
    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的長度
     **/
    public long append(String key, String value) {
      Jedis jedis = getJedis();
      long len = jedis.append(key, value);
      jedis.close();
      return len;
    }

    /**
     * 將key對應(yīng)的value減去指定的值,只有value可以轉(zhuǎn)為數(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對應(yīng)的value加上指定的值,只有value可以轉(zhuǎn)為數(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;
    }

    /**
     * 對指定key對應(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對應(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的對應(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;
    }

    /**
     * 批量存儲記錄
     *
     * @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對應(yīng)的值的長度
     *
     * @param  key
     * @return value值得長度
     */
    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中元素個數(shù)
     *
     * @param  key
     * @return 元素個數(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;
    }

    /**
     * 這個命令等于sdiff,但返回的不是結(jié)果集,而是將結(jié)果集存儲在新的集合中,如果目標(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;
    }

    /**
     * 返回給定集合交集的成員,如果其中一個集合為不存在或?yàn)榭?,則返回空Set
     *
     * @param keys
     * @return 交集成員的集合
     **/
    public Set<String> sinter(String... keys) {
      Jedis jedis = getJedis();
      Set<String> set = jedis.sinter(keys);
      jedis.close();
      return set;
    }

    /**
     * 這個命令等于sinter,但返回的不是結(jié)果集,而是將結(jié)果集存儲在新的集合中,如果目標(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;
    }

    /**
     * 確定一個給定的值是否存在
     *
     * @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 要刪除的成員
     * @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;
    }

    /**
     * 合并多個集合并返回合并后的結(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;
    }

    /**
     * 合并多個集合并將合并后的結(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中刪除指定的存儲
     *
     * @param  key
     * @param fieid 存儲的名字
     * @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;
    }

    /**
     * 測試hash中指定的存儲是否存在
     *
     * @param key
     * @param  fieid 存儲的名字
     * @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中指定存儲位置的值
     *
     * @param key
     * @param fieid 存儲的名字
     * @return 存儲對應(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中的存儲和值
     *
     * @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;
    }

    /**
     * 添加一個對應(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;
    }

    /**
     * 添加對應(yīng)關(guān)系,只有在fieid不存在時才執(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;
    }

    /**
     * 在指定的存儲位置加上指定的數(shù)字,存儲位置的值必須可轉(zhuǎn)為數(shù)字類型
     *
     * @param key
     * @param  fieid 存儲位置
     * @param  value 要增加的值,可以是負(fù)數(shù)
     * @return 增加指定數(shù)字后,存儲位置的值
     */
    public long hincrby(String key, String fieid, long value) {
      Jedis jedis = getJedis();
      long s = jedis.hincrBy(key, fieid, value);
      jedis.close();
      return s;
    }

    /**
     * 返回指定hash中的所有存儲名字,類似Map中的keySet方法
     *
     * @param key
     * @return Set<String> 存儲名稱的集合
     */
    public Set<String> hkeys(String key) {
      // ShardedJedis sjedis = getShardedJedis();
      Jedis sjedis = getJedis();
      Set<String> set = sjedis.hkeys(key);
      sjedis.close();
      return set;
    }

    /**
     * 獲取hash中存儲的個數(shù),類似Map中size方法
     *
     * @param  key
     * @return long 存儲的個數(shù)
     */
    public long hlen(String key) {
      // ShardedJedis sjedis = getShardedJedis();
      Jedis sjedis = getJedis();
      long len = sjedis.hlen(key);
      sjedis.close();
      return len;
    }

    /**
     * 根據(jù)多個key,獲取對應(yīng)的value,返回List,如果指定的key不存在,List對應(yīng)位置為null
     *
     * @param  key
     * @param  fieids 存儲位置
     * @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;
    }

    /**
     * 添加對應(yīng)關(guān)系,如果對應(yīng)關(guān)系已存在,則覆蓋
     *
     * @param key
     * @param map 對應(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;
    }

    /**
     * 添加對應(yīng)關(guān)系,如果對應(yīng)關(guān)系已存在,則覆蓋
     *
     * @param  key
     * @param  map 對應(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長度
     *
     * @param  key
     * @return 長度
     */
    public long llen(String key) {
      return llen(SafeEncoder.encode(key));
    }

    /**
     * List長度
     *
     * @param key
     * @return 長度
     */
    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的相對位置插入記錄
     *
     * @param where 前面插入或后面插入
     * @param pivot 相對位置的內(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 相對位置的內(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 要刪除的數(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 要刪除的數(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則表示最后一個,-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則表示最后一個,-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連接池對象 */
	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連接池對象
	 * @return
	 */
	public JedisPool getJedisPool() {
		return jedisPool;
	}

	/**
	 * 注入Redis連接池對象
	 * @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;
  // 定義接收對象
  List<Area> areaList = null;
  // 定義jackson數(shù)據(jù)轉(zhuǎn)換操作類
  ObjectMapper mapper = new ObjectMapper();
  // 判斷key是否存在
		if (!jedisKeys.exists(key)) {
    // 若不存在,則從數(shù)據(jù)庫里面取出相應(yīng)數(shù)據(jù)
    areaList = areaDao.queryArea();
    // 將相關(guān)的實(shí)體類集合轉(zhuǎn)換成string,存入redis里面對應(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對應(yīng)的value里的的string轉(zhuǎn)換成對象的實(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;
}

感謝各位的閱讀!關(guān)于“IDEA SSM整合Redis項(xiàng)目的示例分析”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI