IDEA SSM 통합 Redis 프로젝트 인 스 턴 스 소스 코드

41074 단어 IDEASSM통합Redis
IDEA SSM 통합 Redis 프로젝트 인 스 턴 스
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       -->
	<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
		<!--     pool      jedis   -->
		<property name="maxTotal" value="${redis.pool.maxActive}" />
		<!--          maxIdle    ,     20,                  20     ,     ,        。 -->
		<property name="maxIdle" value="${redis.pool.maxIdle}" />
		<!--       :        ,               (     ),          -->
		<property name="maxWaitMillis" value="${redis.pool.maxWait}" />
		<!--               -->
		<property name="testOnBorrow" value="${redis.pool.testOnBorrow}" />
	</bean>

	<!--   Redis   ,       -->
	<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>

	<!--   Redis   ,   Redis            -->
	<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     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.자바

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;
  /**
   *       String     
   */
  public Strings STRINGS;
  /**
   *       List     
   */
  public Lists LISTS;
  /**
   *       Set     
   */
  public Sets SETS;
  /**
   *       HashMap     
   */
  public Hash HASH;

  /**
   * Redis     
   */
  private JedisPool jedisPool;

  /**
   *   redis   
   */
  public JedisPool getJedisPool() {
    return jedisPool;
  }

  /**
   *   redis   
   */
  public void setJedisPool(JedisPoolWriper jedisPoolWriper) {
    this.jedisPool = jedisPoolWriper.getJedisPool();
  }

  /**
   *  jedis        jedis  
   */
  public Jedis getJedis() {
    return jedisPool.getResource();
  }

  /**
   *       
   *
   * @author xiangze
   */
  public void expire(String key, int seconds) {
    if (seconds <= 0) {
      return;
    }
    Jedis jedis = getJedis();
    jedis.expire(key, seconds);
    jedis.close();
  }

  /**
   *         
   *
   * @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    
     */
    public String rename(String oldkey, String newkey) {
      return rename(SafeEncoder.encode(oldkey), SafeEncoder.encode(newkey));
    }

    /**
     *   key,   key       
     *
     * @return    
     */
    public long renamenx(String oldkey, String newkey) {
      Jedis jedis = getJedis();
      long status = jedis.renamenx(oldkey, newkey);
      jedis.close();
      return status;
    }

    /**
     *   key
     *
     * @return    
     */
    public String rename(byte[] oldkey, byte[] newkey) {
      Jedis jedis = getJedis();
      String status = jedis.rename(oldkey, newkey);
      jedis.close();
      return status;
    }

    /**
     *   key     ,     
     * *
     */
    public long expired(String key, int seconds) {
      Jedis jedis = getJedis();
      long count = jedis.expire(key, seconds);
      jedis.close();
      return count;
    }

    /**
     *   key     ,     (          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       
     *
     * @return       
     */
    public long persist(String key) {
      Jedis jedis = getJedis();
      long count = jedis.persist(key);
      jedis.close();
      return count;
    }

    /**
     *   keys     ,     key
     *
     * @param  keys
     * @return       
     */
    public long del(String... keys) {
      Jedis jedis = getJedis();
      long count = jedis.del(keys);
      jedis.close();
      return count;
    }

    /**
     *   keys     ,     key
     *
     * @param  keys
     * @return       
     */
    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    ,                 
     *
     * @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     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    ,*    ,?    
     */
    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) {
    }

    /**
     *   key    
     *
     * @param  key
     * @return  
     */
    public String get(String key) {
      // ShardedJedis sjedis = getShardedJedis();
      Jedis sjedis = getJedis();
      String value = sjedis.get(key);
      sjedis.close();
      return value;
    }

    /**
     *   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    
     */
    public String set(String key, String value) {
      return set(SafeEncoder.encode(key), SafeEncoder.encode(value));
    }

    /**
     *     ,             value
     *
     * @param  key
     * @param  value
     * @return    
     */
    public String set(String key, byte[] value) {
      return set(SafeEncoder.encode(key), value);
    }

    /**
     *     ,             value
     *
     * @param  key
     * @param  value
     * @return    
     */
    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     
     */
    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     
     */
    public String setEx(byte[] key, int seconds, byte[] value) {
      Jedis jedis = getJedis();
      String str = jedis.setex(key, seconds, value);
      jedis.close();
      return str;
    }

    /**
     *       ,     key       
     *
     * @param  key
     * @param  value
     * @return long    ,1     key   ,0   ,key  
     */
    public long setnx(String key, String value) {
      Jedis jedis = getJedis();
      long str = jedis.setnx(key, value);
      jedis.close();
      return str;
    }

    /**
     *            ,                 <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   value      ,  value             
     *
     * @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   value      ,  value             
     *
     * @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   value    
     *
     * @param  key
     * @param  startOffset     (  )
     * @param endOffset     (  )
     * @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;
    }

    /**
     *        key   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       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    
     */
    public String mset(String... keysvalues) {
      Jedis jedis = getJedis();
      String str = jedis.mset(keysvalues);
      jedis.close();
      return str;
    }

    /**
     *   key       
     *
     * @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     
     *
     * @param  key
     * @return     
     */
    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,         ,              ,       ,   。
     *
     * @param newkey      key
     * @param keys      
     * @return         
     **/
    public long sdiffstore(String newkey, String... keys) {
      Jedis jedis = getJedis();
      long s = jedis.sdiffstore(newkey, keys);
      jedis.close();
      return s;
    }

    /**
     *            ,               ,    Set
     *
     * @param keys
     * @return        
     **/
    public Set<String> sinter(String... keys) {
      Jedis jedis = getJedis();
      Set<String> set = jedis.sinter(keys);
      jedis.close();
      return set;
    }

    /**
     *       sinter,         ,              ,       ,   。
     *
     * @param newkey      key
     * @param  keys      
     * @return         
     **/
    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;
    }

    /**
     *                 <br/>
     *                 ,       ,  0<br/>
     *             ,        ,            ,          
     *
     * @param  srckey    
     * @param dstkey     
     * @param member        
     * @return    ,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    ,    1,       0
     */
    public long srem(String key, String member) {
      Jedis jedis = getJedis();
      long s = jedis.srem(key, member);
      jedis.close();
      return s;
    }

    /**
     *                ,            <br/>
     *
     * @param   keys
     * @return         
     * @see
     */
    public Set<String> sunion(String... keys) {
      Jedis jedis = getJedis();
      Set<String> set = jedis.sunion(keys);
      jedis.close();
      return set;
    }

    /**
     *                          ,            
     *
     * @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    ,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       
     */
    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;
    }

    /**
     *         
     *
     * @param  key
     * @param  fieid
     * @param value
     * @return     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;
    }

    /**
     *       ,   fieid       
     *
     * @param  key
     * @param fieid
     * @param  value
     * @return     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;
    }

    /**
     *                ,               
     *
     * @param key
     * @param  fieid     
     * @param  value      ,     
     * @return        ,      
     */
    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      ,  Map size  
     *
     * @param  key
     * @return long      
     */
    public long hlen(String key) {
      // ShardedJedis sjedis = getShardedJedis();
      Jedis sjedis = getJedis();
      long len = sjedis.hlen(key);
      sjedis.close();
      return len;
    }

    /**
     *     key,     value,  List,     key   ,List     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;
    }

    /**
     *       ,         ,   
     *
     * @param key
     * @param map     
     * @return   ,    OK
     */
    public String hmset(String key, Map<String, String> map) {
      Jedis jedis = getJedis();
      String s = jedis.hmset(key, map);
      jedis.close();
      return s;
    }

    /**
     *       ,         ,   
     *
     * @param  key
     * @param  map     
     * @return   ,    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    
     */
    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    
     */
    public String lset(String key, int index, String value) {
      return lset(SafeEncoder.encode(key), index, SafeEncoder.encode(value));
    }

    /**
     *  value         
     *
     * @param where          
     * @param pivot        
     * @param  value      
     * @return     
     */
    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        
     * @param  value      
     * @return     
     */
    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     
     */
    public long lpush(String key, String value) {
      return lpush(SafeEncoder.encode(key), SafeEncoder.encode(value));
    }

    /**
     *  List      
     *
     * @param  key
     * @param value
     * @return     
     */
    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     
     */
    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     
     */
    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      ,       
     * @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       ,       List             
     * @param  value      
     * @return     List     
     */
    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       ,       List             
     * @param  value      
     * @return     List     
     */
    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        (   -1       ,-2,-3    )
     * @return      
     */
    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        (   -1       ,-2,-3    )
     * @return      
     */
    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;

/**
 *    redis JedisPool      ,     centos    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,그리고 우리 제 디 스 사용 할 수 있 습 니 다
우리 의 업무 논리 에서 자주 바 꿀 필요 가 없 는 데 이 터 를 redis 에 저장 합 니 다.예 를 들 어 다음 부분 코드:

 @Transactional
  public List<Area> getAreaList(){//   redis key
  String key = AREALISTKEY;
  //       
  List<Area> areaList = null;
  //   jackson       
  ObjectMapper mapper = new ObjectMapper();
  //   key    
		if (!jedisKeys.exists(key)) {
    //     ,             
    areaList = areaDao.queryArea();
    //             string,  redis     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        
    String jsonString = jedisStrings.get(key);
    //     string        
    JavaType javaType = mapper.getTypeFactory().constructParametricType(ArrayList.class, Area.class);
    try {
      //    key   value   string           
      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 프로젝트 인 스 턴 스 의 상세 한 내용 입 니 다.IDEA SSM 통합 Redis 에 관 한 자 료 는 저희 의 다른 관련 글 을 주목 하 세 요!

좋은 웹페이지 즐겨찾기