Redis+Jedis+Spring封装RedisManager类操作缓存

Redis作为一个key-value存储系统。与和Memcached相比,它支持存储的value类型更多,有string、list、set、zset(sorted set )和hash
针对这些类型,Redis命令也比较多:

Redis+Jedis+Spring封装RedisManager类操作缓存_第1张图片

而在代码中使用jedis就可以操作这些命令实现存储。
本博客打算封装一个个RedisManger类,用于实现操作缓存的命令。
Redis配置文件:

cache.redis.servers=127.0.0.1
cache.redis.port=6379
cache.redis.maxActive=300
cache.redis.maxIdle=200
cache.redis.maxWaitMillis=3000
cache.redis.testOnBorrow=true
cache.redis.testOnReturn=true
   
   
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

spring管理的配置文件:


<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"
       xmlns:jms="http://www.springframework.org/schema/jms"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-2.5.xsd
        http://www.springframework.org/schema/jms
        http://www.springframework.org/schema/jms/spring-jms-2.5.xsd" >
      <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath:Redis.propertiesvalue>
            list>
        property>
     bean>
     <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">  
        <property  name="maxTotal" value="${cache.redis.maxActive}" />  
        <property  name="maxIdle" value="${cache.redis.maxIdle}" />  
        <property  name="maxWaitMillis" value="${cache.redis.maxWaitMillis}" />  
        <property  name="testOnBorrow" value="${cache.redis.testOnBorrow}" />  
    bean>

    <bean id="redisPool" class="com.test.cache.redis.RedisPool"  init-method="init">
        <property name="config" ref="jedisPoolConfig" />
        <property name="serverIp" value="${cache.redis.servers}" />
        <property name="port" value="${cache.redis.port}" />
    bean>

    <bean id="redisManager" class="com.test.cache.redis.RedisManager">  
        <property name="redisPool" ref="redisPool" />  
    bean>

beans>
   
   
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36

连接池代码:

package com.test.cache.redis;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

public class RedisPool {

    private JedisPoolConfig config; 
    private String serverIp;
    private int port;
    private JedisPool pool;

    public void init() {
        pool = new JedisPool(config, serverIp, port, 4000);
    }

    public Jedis getInstance() {
        return pool.getResource();
    }

    public void returnResource(Jedis jedis) {
        pool.returnResource(jedis);
    }

    public void returnBrokenResource(Jedis jedis){
        pool.returnBrokenResource(jedis);
    }

    public JedisPoolConfig getConfig() {
        return config;
    }

    public void setConfig(JedisPoolConfig config) {
        this.config = config;
    }

    public String getServerIp() {
        return serverIp;
    }

    public void setServerIp(String serverIp) {
        this.serverIp = serverIp;
    }

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

}
   
   
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54

封装的RedisManager类:

package com.test.cache.redis;
import java.util.List;
import java.util.Map;
import java.util.Set;

import redis.clients.jedis.*;
public class RedisManager {

    private RedisPool redisPool;

    public Jedis getJedis() {
        return redisPool.getInstance();
    }

    public void releaseJedis(Jedis jedis) {
        redisPool.returnResource(jedis);
    }

    public void releaseBrokenJedis(Jedis jedis) {
        redisPool.returnBrokenResource(jedis);
    }

    /**hash
     * 通过key给field设置指定的值,如果key不存在,则先创建 ,存在会覆盖原来的值
     * @param key
     * @param field字段
     * @param value
     * @return 如果不存在,新建的返回1,存在返回0, 异常返回null
     * 
     */
    public Long hset(String key, String field, String value) {
        Jedis jedis = getJedis();
        Long result = jedis.hset(key, field, value);
        releaseJedis(jedis);
        return result;
    }
    /**Hash
     * 为哈希表 key 中的域 field 的值加上增量 value
     * @param key
     * @param field
     * @param value
     * @return
     */
    public Long hincrBy(String key, String field, long value) {
        Jedis jedis = getJedis();
        Long result = jedis.hincrBy(key, field, value);
        releaseJedis(jedis);
        return result;
    }

    /**

     * 通过key给field设置指定的值,如果key不存在则先创建,如果field已经存在,操作无效
     * @param key
     * @param field
     * @param value
     * @return 不存在新建返回1,存在返回0
     */
    public Long hsetnx(String key, String field, String value) {
        Jedis jedis = getJedis();
        Long result = jedis.hsetnx(key, field, value);
        releaseJedis(jedis);
        return result;
    }

    /**
     * 通过key同时设置 hash的多个field
     * @param key
     * @param hash
     * @return 返回OK 异常返回null
     */
    public String hmset(String key, Map hash) {
        Jedis jedis = getJedis();
        String result = jedis.hmset(key, hash);
        releaseJedis(jedis);
        return result;
    }

    /**
     * 通过key 和 field 获取指定的 value
     * @param key
     * @param field
     * @return 没有返回null
     */
    public String hget(String key, String field) {
        Jedis jedis = getJedis();
        String result = jedis.hget(key, field);
        releaseJedis(jedis);
        return result;
    }

    /**
     * 通过key 和 fields 获取指定的value 如果没有对应的value则返回null
     * @param key
     * @param fields可以使 一个String 也可以是 String数组
     * @return
     */
    public List hmget(String key, String... fields) {
        Jedis jedis = getJedis();
        List result = jedis.hmget(key, fields);
        releaseJedis(jedis);
        return result;
    }

    /**
     * 通过key获取所有的field和value
     * @param key
     * @return
     */
    public Map hgetAll(String key) {
        Jedis jedis = getJedis();
        Map result = jedis.hgetAll(key);
        releaseJedis(jedis);
        return result;
    }
    /**
     * 通过key删除field的value
     * @param key
     * @return
     */
    public Long hdel(String key, String field) {
        Jedis jedis = getJedis();
        Long result = jedis.hdel(key, field);
        releaseJedis(jedis);
        return result;
    }
    /**
     * 返回key为键中存放的field值的个数
     * @param key
     * @return 
     */
    public Long hlen(String key) {
        Jedis jedis = getJedis();
        Long result = jedis.hlen(key);
        releaseJedis(jedis);
        return result;
    }
    /**
     * 查看key是否存在指定的field
     * @param key
     * @return 
     */
    public Boolean hexists(String key, String field) {
        Jedis jedis = getJedis();
        Boolean result = jedis.hexists(key, field);
        releaseJedis(jedis);
        return result;
    }

    /**
     * 返回key存储的map对象中的所有key  
     * @param key
     * @return 
     */
    public Set hkeys(String key) {
        Jedis jedis = getJedis();
        Set result = jedis.hkeys(key);
        releaseJedis(jedis);
        return result;
    }
    /**
     * 返回key存储的map对象中的所有键的values值  
     * @param key
     * @return 
     */
    public List hvals(String key) {
        Jedis jedis = getJedis();
        List result = jedis.hvals(key);
        releaseJedis(jedis);
        return result;
    }

    /**
     * 判断key是否存在
     * @param key
     * @return true OR false
     */
    public boolean exists(String key) {
        Jedis jedis = null;
        boolean result = false;
        try {
            jedis = getJedis();
            result = jedis.exists(key);
        } catch (Exception e) {
            releaseBrokenJedis(jedis);
            e.printStackTrace();
        } finally {
            releaseJedis(jedis);
        }

        return result;
    }

    /**
     * 删除指定的key,也可以传入一个包含key的数组
     * @param keys
     * @return 返回删除成功的个数
     */
    public Long del(String... keys) {
        Jedis jedis = null;
        Long result = 0L;
        try {
            jedis = getJedis();
            result = jedis.del(keys);
        } catch (Exception e) {
            releaseBrokenJedis(jedis);
            e.printStackTrace();
            result = 0L;
        } finally {
            releaseJedis(jedis);
        }
        return result;
    }

    /**
     * 对key的对应value值排序
     * @return 
     */
    public List sort(String key) {
        Jedis jedis = getJedis();
        List result = jedis.sort(key);
        releaseJedis(jedis);
        return result;
    }
    /**
     * 将当前数据库的 ke移动到给定的数据库 db 当中
     * @return 
     */
    public Long move(String key, int dbIndex) {
        Jedis jedis = getJedis();
        Long result = jedis.move(key, dbIndex);
        releaseJedis(jedis);
        return result;
    }
    /**
     * 返回某个key元素的数据类型 ( none:不存在,string:字符,list,set,zset,hash)
     * @return 
     */
    public String type(String key) {
        Jedis jedis = getJedis();
        String result = jedis.type(key);
        releaseJedis(jedis);
        return result;
    }
    /**
     * 返回当前数据库的key的总数
     * @return 
     */
    public Long dbsize() {
        Jedis jedis = getJedis();
        Long result = jedis.dbSize();
        releaseJedis(jedis);
        return result;
    }
    /**
     *设置某个key的过期时间(秒),(EXPIRE bruce 1000:设置bruce这个key1000秒后系统自动删除)注意:如果在还没有过期的时候,对值进行了改变,那么那个值会被清除。
     * @return 
     */
    public Long expire(String key, int seconds) {
        Jedis jedis = getJedis();
        Long result = jedis.expire(key, seconds);
        releaseJedis(jedis);
        return result;
    }
    /**
     * EXPIREAT 的作用和 EXPIRE 类似,都用于为 key 设置生存时间。
     * 不同在于 EXPIREAT 命令接受的时间参数是 UNIX 时间戳(unix timestamp)。
     * @return 
     */

    public Long expireAt(String key, Long unixTime) {
        Jedis jedis = getJedis();
        Long result = jedis.expireAt(key, unixTime);
        releaseJedis(jedis);
        return result;
    }
    /**List
     * 通过key在list头部添加值
     * @param key
     * @param value
     * @return 在 push 操作后的 list 长度。
     */
    public Long lpush(String key, String value) {
        Jedis jedis = getJedis();
        Long result = jedis.lpush(key, value);
        releaseJedis(jedis);
        return result;
    }
    /**List
     * 向存于 key 的列表的尾部插入所有指定的值。如果 key 不存在,那么会创建一个空的列表然后再进行 push 操作。 
     * 当 key 保存的不是一个列表,那么会返回一个错误。
     * @param key
     * @param value
     * @return  在 push 操作后的列表长度
     */
    public Long rpush(String key, String value) {
        Jedis jedis = getJedis();
        Long result = jedis.rpush(key, value);
        releaseJedis(jedis);
        return result;
    }
    /**List
     * 获取list的长度
     * @param key
     * @return 
     */
    public Long llen(String key) {
        Jedis jedis = getJedis();
        Long result = jedis.llen(key);
        releaseJedis(jedis);
        return result;
    }
    /**List
     * 返回存储在 key 的列表里指定范围内的元素
     * @param key 
     * @parm start 开始位置
     * @param end 结束位置 -1表示最后一个
     * @return 
     */
    public List lrange(String key, long start, long end) {
        Jedis jedis = getJedis();
        List result = jedis.lrange(key, start, end);
        releaseJedis(jedis);
        return result;
    }
    /**List
     * 截取(trim)一个已存在的 list,这样 list 就会只包含指定范围的指定元素
     * @param key 
     * @parm start 开始位置
     * @param end 结束位置 -1表示最后一个
     * @return 
     */

    public String ltrim(String key, long start, long end) {
        Jedis jedis = getJedis();
        String result = jedis.ltrim(key, start, end);
        releaseJedis(jedis);
        return result;
    }
    /**List
     * 通过key在list头部添加值
     * 只有当 key 已经存在并且存着一个 list 的时候,在这个 key 下面的 list 的头部插入 value。 与 LPUSH 相反,当 key 不存在的时候不会进行任何操作。
     * @param key
     * @param value
     * @return 在 push 操作后的 list 长度。
     */

    public Long lpushx(String key, String value) {
        Jedis jedis = getJedis();
        Long result = jedis.lpushx(key, value);
        releaseJedis(jedis);
        return result;
    }

    public Long rpushx(String key, String value) {
        Jedis jedis = getJedis();
        Long result = jedis.rpushx(key, value);
        releaseJedis(jedis);
        return result;
    }
    /**
     * 弹出 List 的第一个元素
     * @param key
     * @return
     */
    public String lpop(String key) {
        Jedis jedis = getJedis();
        String result = jedis.lpop(key);
        releaseJedis(jedis);
        return result;
    }

    public String rpop(String key) {
        Jedis jedis = getJedis();
        String result = jedis.rpop(key);
        releaseJedis(jedis);
        return result;
    }
    /**
     * 根据参数 COUNT 的值,移除列表中与参数 VALUE 相等的元素。
     * count > 0 : 从表头开始向表尾搜索,移除与 VALUE 相等的元素,数量为 COUNT 。
     * count < 0 : 从表尾开始向表头搜索,移除与 VALUE 相等的元素,数量为 COUNT 的绝对值。
     * count = 0 : 移除表中所有与 VALUE 相等的值。
     */

    public Long lrem(String key, long count, String value) {
        Jedis jedis = getJedis();
        Long result = jedis.lrem(key, count, value);
        releaseJedis(jedis);
        return result;
    }
    /**
     * 设置 index 位置的list元素的值为 value
     * @param key
     * @param index
     * @param value
     * @return
     */
    public String lset(String key, long index, String value) {
        Jedis jedis = getJedis();
        String result = jedis.lset(key, index, value);
        releaseJedis(jedis);
        return result;
    }
    /**
     * 返回列表 key 中,下标为 index 的元素。
     * @param key
     * @param index
     * @return
     */
    public String lindex(String key, long index) {
        Jedis jedis = getJedis();
        String result = jedis.lindex(key, index);
        releaseJedis(jedis);
        return result;
    }
   /**
    *命令 RPOPLPUSH 在一个原子时间内,执行以下两个动作:
    *将列表 source 中的最后一个元素(尾元素)弹出,并返回给客户端。
    *将 source 弹出的元素插入到列表 destination ,作为 destination 列表的的头元素。
    *举个例子,你有两个列表 source 和 destination , source 列表有元素 a, b, c , destination 列表有元素 x, y, z ,
    *执行 RPOPLPUSH source destination 之后, source 列表包含元素 a, b , destination 列表包含元素 c, x, y, z ,
    *并且元素 c 会被返回给客户端。
    * @param srcKey
    * @param dstKey
    * @return
    */
    public String rpoplpush(String srcKey, String dstKey) {
        Jedis jedis = getJedis();
        String result = jedis.rpoplpush(srcKey, dstKey);
        releaseJedis(jedis);
        return result;
    }
    /**
     * BRPOPLPUSH 是 RPOPLPUSH 的阻塞版本,当给定列表 source 不为空时, BRPOPLPUSH 的表现和 RPOPLPUSH 一样。
     *当列表 source 为空时, BRPOPLPUSH 命令将阻塞连接,直到等待超时,或有另一个客户端对 source 执行 LPUSH 或 RPUSH 命令为止。
     *超时参数 timeout 接受一个以秒为单位的数字作为值。超时参数设为 0 表示阻塞时间可以无限期延长(block indefinitely) 。
     * @param source
     * @param destination
     * @param timeout
     * @return
     */
    public String brpoplpush(String source, String destination, int timeout) {
        Jedis jedis = getJedis();
        String result = jedis.brpoplpush(source, destination, timeout);
        releaseJedis(jedis);
        return result;
    }


   /**
    * 将一个或多个 member 元素加入到集合 key 当中,已经存在于集合的 member 元素将被忽略(set不重复)。
    * 假如 key 不存在,则创建一个只包含 member 元素作成员的集合。
    * @param key
    * @param member
    * @return
    */
    public Long sadd(String key, String member) {
        Jedis jedis = getJedis();
        Long result = jedis.sadd(key, member);
        releaseJedis(jedis);
        return result;
    }
    /**
     * 移除集合 key 中的一个 member 元素,不存在的 member 元素会被忽略。
     * @param key
     * @param member
     * @return
     */
    public Long srem(String key, String member) {
        Jedis jedis = getJedis();
        Long result = jedis.srem(key, member);
        releaseJedis(jedis);
        return result;
    }
    /**
     * 返回集合 key 中的所有成员。
     * @param key
     * @return
     */
    public Set smembers(String key) {
        Jedis jedis = getJedis();
        Set result = jedis.smembers(key);
        releaseJedis(jedis);
        return result;
    }
     /**
      * 判断 member 元素是否集合 key 的成员。
      * @param key
      * @param member
      * @return
      */
    public Boolean sismember(String key, String member) {
        Jedis jedis = getJedis();
        Boolean result = jedis.sismember(key, member);
        releaseJedis(jedis);
        return result;
    }
   /**
    * 返回集合 key集合中元素的数量)。
    * @param key
    * @return
    */
    public Long scard(String key) {
        Jedis jedis = getJedis();
        Long result = jedis.scard(key);
        releaseJedis(jedis);
        return result;
    }
    /**
     * 将 member 元素从 source 集合移动到 destination 集合。
     *SMOVE 是原子性操作。
     *如果 source 集合不存在或不包含指定的 member 元素,则 SMOVE 命令不执行任何操作,仅返回 0 。否则, member 元素从 source 集合中被移除,并添加到 destination 集合中去。
     *当 destination 集合已经包含 member 元素时, SMOVE 命令只是简单地将 source 集合中的 member 元素删除。
     *当 source 或 destination 不是集合类型时,返回一个错误。
     * @param srckey
     * @param dstkey
     * @param member
     * @return
     */
    public Long smove(String srckey, String dstkey, String member) {
        Jedis jedis = getJedis();
        Long result = jedis.smove(srckey, dstkey, member);
        releaseJedis(jedis);
        return result;
    }

    /**
     * 移除并返回集合中的一个随机元素。
     * @param key
     * @return
     */
    public String spop(String key) {
        Jedis jedis = getJedis();
        String result = jedis.spop(key);
        releaseJedis(jedis);
        return result;
    }
     /**
      * 返回集合中的一个随机元素
      * @param key
      * @return
      */
    public String srandmember(String key) {
        Jedis jedis = getJedis();
        String result = jedis.srandmember(key);
        releaseJedis(jedis);
        return result;
    }

    /**
     * 返回给定集合的交集。
     * @param keys
     * @return
     */
    public Set sinter(String... keys) {
        Jedis jedis = getJedis();
        Set result = jedis.sinter(keys);
        releaseJedis(jedis);
        return result;
    }
    /**
     * 类似于 SINTER 命令,但它将结果保存到 destination 集合,而不是简单地返回结果集
     * @param dstkey
     * @param keys
     * @return
     */
    public Long sinterstore(String dstkey, String... keys) {
        Jedis jedis = getJedis();
        Long result = jedis.sinterstore(dstkey, keys);
        releaseJedis(jedis);
        return result;
    }
   /**
    * 返回所有给定集合的并集
    * @param keys
    * @return
    */
    public Set sunion(String... keys) {
        Jedis jedis = getJedis();
        Set result = jedis.sunion(keys);
        releaseJedis(jedis);
        return result;
    }

    public Long sunionstore(String dstkey, String... keys) {
        Jedis jedis = getJedis();
        Long result = jedis.sunionstore(dstkey, keys);
        releaseJedis(jedis);
        return result;
    }
    /**
     * 返回所有给定集合之间的差集。
     * @param keys
     * @return
     */
    public Set sdiff(String... keys) {
        Jedis jedis = getJedis();
        Set result = jedis.sdiff(keys);
        releaseJedis(jedis);
        return result;
    }

    public Long sdiffstore(String dstkey, String... keys) {
        Jedis jedis = getJedis();
        Long result = jedis.sdiffstore(dstkey, keys);
        releaseJedis(jedis);
        return result;
    }
   /**server
    * 清空整个 Redis 服务器的数据(删除所有数据库的所有 key )。
    * @return
    */
    public String flushAll() {
        Jedis jedis = getJedis();
        String result = jedis.flushAll();
        releaseJedis(jedis);
        return result;
    }
   /**
    * 清空当前数据库中的所有 key。
    * @return
    */
    public String flushDB() {
        Jedis jedis = getJedis();
        String result = jedis.flushDB();
        releaseJedis(jedis);
        return result;
    }
    /** 
    *停止所有客户端
    *如果有至少一个保存点在等待,执行 SAVE 命令
    *如果 AOF 选项被打开,更新 AOF 文件
    * 关闭 redis 服务器(server)
    * @return
    */

    public String shutdown() {
        Jedis jedis = getJedis();
        String result = jedis.shutdown();
        releaseJedis(jedis);
        return result;
    }

    /**sorted set
     * 将一个 member 元素及其 score值加入到有序集 key 当中。
     * 如果某个 member 已经是有序集的成员,那么更新这个 member 的 score 值,
     *并通过重新插入这个 member 元素,来保证该 member 在正确的位置上。
     * @param key
     * @param score
     * @param member
     * @return
     */
    public Long zadd(String key, double score, String member) {
        Jedis jedis = getJedis();
        Long result = jedis.zadd(key, score, member);
        releaseJedis(jedis);
        return result;
    }
    /**
     * sorted set
     * 移除有序集 key 中的一成员member,不存在的成员将被忽略。
     * @param key
     * @param member
     * @return
     */
    public Long zrem(String key, String member) {
        Jedis jedis = getJedis();
        Long result = jedis.zrem(key, member);
        releaseJedis(jedis);
        return result;
    }
    /**sorted set
     * 返回集合 key集合中元素的数量
     * @param key
     * @return
     */
    public Long zcard(String key) {
        Jedis jedis = getJedis();
        Long result = jedis.zcard(key);
        releaseJedis(jedis);
        return result;
    }
    /**
     * 返回有序集 key 中, score 值在 min 和 max 之间(默认包括 score 值等于 min 或 max )的成员的数量。
     * @param key
     * @param min
     * @param max
     * @return
     */
    public Long zcount(String key, double min, double max) {
        Jedis jedis = getJedis();
        Long result = jedis.zcount(key, min, max);
        releaseJedis(jedis);
        return result;
    }
    /**
     * 返回有序集 key 中,成员 member 的 score 值。
     * @param key
     * @param member
     * @return
     */
    public Double zscore(String key, String member) {
        Jedis jedis = getJedis();
        Double result = jedis.zscore(key, member);
        releaseJedis(jedis);
        return result;
    }
    /**
     * 为有序集 key 的成员 member 的 score 值加上增量"score"
     * @param key
     * @param score
     * @param member
     * @return
     */
    public Double zincrby(String key, double score, String member) {
        Jedis jedis = getJedis();
        Double result = jedis.zincrby(key, score, member);
        releaseJedis(jedis);
        return result;
    }
   /**
    * 返回有序集 key 中,指定区间内的成员。其中成员的位置按 score 值递增(从小到大)来排序。
    * @param key
    * @param start
    * @param end
    * @return
    */
    public Set zrange(String key, int start, int end) {
        Jedis jedis = getJedis();
        Set result = jedis.zrange(key, start, end);
        releaseJedis(jedis);
        return result;
    }
    /**
     * 其中成员的位置按 score 值递减(从大到小)来排列。
     * @param key
     * @param start
     * @param end
     * @return
     */
    public Set zrevrange(String key, int start, int end) {
        Jedis jedis = getJedis();
        Set result = jedis.zrevrange(key, start, end);
        releaseJedis(jedis);
        return result;
    }
    /**
     * 返回有序集 key 中, score 值介于 max 和 min 之间(默认包括等于 max 或 min )的所有的成员。
     * 有序集成员按 score 值递减(从大到小)的次序排列。
     * @param key
     * @param max
     * @param min
     * @param offset
     * @param count
     * @return
     */
    public Set zrevrangeByScore(String key, double max, double min,int offset, int count) {
        Jedis jedis=getJedis();
        try{
            Set result=jedis.zrevrangeByScore(key, max, min, offset, count);
            return result;
        }finally{
            releaseJedis(jedis);
        }
    }

    public Set zrevrangeByScore(String key, double max, double min) {
        Jedis jedis=getJedis();
        try{
            Set result=jedis.zrevrangeByScore(key, max, min);
            return result;
        }finally{
            releaseJedis(jedis);
        }
    }
    /**
     * 返回有序集 key 中,所有 score 值介于 min 和 max 之间(包括等于 min 或 max )的成员。
     * 有序集成员按 score 值递增(从小到大)次序排列。
     * @param key
     * @param min
     * @param max
     * @return
     */
    public Set zrangeByScore(String key, double min, double max) {
        Jedis jedis=getJedis();
        try{
            Set result=jedis.zrangeByScore(key, min, max);
            return result;
        }finally{
            releaseJedis(jedis);
        }
    }
    /**
     * 返回有序集 key 中成员 member 的排名。其中有序集成员按 score 值递增(从小到大)顺序排列。
     *  排名以 0 为底,也就是说, score 值最小的成员排名为 0 。
     */

    public Long zrank(String key, String member) {
        Jedis jedis = getJedis();
        Long result = jedis.zrank(key, member);
        releaseJedis(jedis);
        return result;
    }
    /**
     * 返回有序集 key 中成员 member 的排名。其中有序集成员按 score 值递减(从大到小)排序。
     * 排名以 0 为底,也就是说, score 值最大的成员排名为 0 。
     * @param key
     * @param member
     * @return
     */
    public Long zrevrank(String key, String member) {
        Jedis jedis = getJedis();
        Long result = jedis.zrevrank(key, member);
        releaseJedis(jedis);
        return result;
    }
    /**
     * 移除有序集 key 中,指定排名(rank)区间内的所有成员。
     * @param key
     * @param start
     * @param end
     * @return
     */
    public Long zremrangeByRank(String key, int start, int end) {
        Jedis jedis = getJedis();
        Long result = jedis.zremrangeByRank(key, start, end);
        releaseJedis(jedis);
        return result;
    }
    /**
     * 移除有序集 key 中,所有 score 值介于 min 和 max 之间(包括等于 min 或 max )的成员。
     * @param key
     * @param start
     * @param end
     * @return
     */

    public Long zremrangeByScore(String key, double start, double end) {
        Jedis jedis = getJedis();
        Long result = jedis.zremrangeByScore(key, start, end);
        releaseJedis(jedis);
        return result;
    }
    /**
     * 计算给定的一个或多个有序集的交集,并将该交集(结果集)储存到 destination 。
     * 默认情况下,结果集中某个成员的 score 值是所有给定集下该成员 score 值之和.
     * @param dstkey
     * @param sets
     * @return
     */
    public Long zinterstore(String dstkey, String... sets) {
        Jedis jedis = getJedis();
        Long result = jedis.zinterstore(dstkey, sets);
        releaseJedis(jedis);
        return result;
    }
    /**
     * 计算给定的一个或多个有序集的并集,并将该并集(结果集)储存到 destination 。
     * 默认情况下,结果集中某个成员的 score 值是所有给定集下该成员 score 值之 和 。
     * @param dstkey
     * @param sets
     * @return
     */
    public Long zunionstore(String dstkey, String... sets) {
        Jedis jedis = getJedis();
        Long result = jedis.zunionstore(dstkey, sets);
        releaseJedis(jedis);
        return result;
    }

    /**String
     * 通过key获取储存在redis中的value
     * 并释放连接
     * @param key
     * @return 成功返回value 失败返回null
     */
    public String get(String key) {
        Jedis jedis = null;
        String result = null;
        try {
            jedis = getJedis();
            result = jedis.get(key);
        } catch (Exception e) {
            releaseBrokenJedis(jedis);
            e.printStackTrace();
        } finally {
            releaseJedis(jedis);
        }
        return result;
    }

    /**string
     * 向redis存入key和value,并释放连接资源
     * 如果key已经存在 则覆盖
     * @param key
     * @param value
     * @return 成功 返回OK 失败返回 0
     */
    public String set(String key, String value) {
        Jedis jedis = null;
        String result = null;
        try {
            jedis = getJedis();
            result = jedis.set(key, value);
        } catch (Exception e) {
            releaseBrokenJedis(jedis);
            e.printStackTrace();
            result = "0";
        } finally {
            releaseJedis(jedis);
        }
        return result;
    }

    /**
     * 

* 设置key value,如果key已经存在则返回0,nx==> not exist * @param key * @param value * @return 成功返回1 如果存在 和 发生异常 返回 0 */ public Long setnx(String key, String value) { Jedis jedis = null; Long result = 0L; try { jedis = getJedis(); result = jedis.setnx(key, value); } catch (Exception e) { releaseBrokenJedis(jedis); e.printStackTrace(); } finally { releaseJedis(jedis); } return result; } /**string * 将给定 key 的值设为 value ,并返回 key 的旧值(old value)。 * @param key * @param value * @return */ public String getSet(String key, String value) { Jedis jedis = getJedis(); String result = jedis.getSet(key, value); releaseJedis(jedis); return result; } /** * 返回所有(一个或多个)给定 key 的值。 * 如果给定的 key 里面,有某个 key 不存在,那么这个 key 返回特殊值 nil 。因此,该命令永不失败。 * @param keys * @return */ public List mget(String[] keys) { Jedis jedis = getJedis(); List result = jedis.mget(keys); releaseJedis(jedis); return result; } /** * 同时设置一个或多个 key-value 对。 * 有会覆盖 * @param keysvalues */ public void mset(String... keysvalues) { Jedis jedis = getJedis(); jedis.mset(keysvalues); releaseJedis(jedis); } /** * key不存在时才插入 * @param keysvalues */ public void msetnx(String... keysvalues) { Jedis jedis = getJedis(); jedis.msetnx(keysvalues); releaseJedis(jedis); } /** * 将 key 所储存的值加上增量 increment 。 * 如果 key 不存在,那么 key 的值会先被初始化为 0 ,然后再执行 INCRBY 命令。 * @param key * @param integer * @return */ public Long incrBy(String key, Integer integer) { Jedis jedis = getJedis(); Long result = jedis.incrBy(key, integer); releaseJedis(jedis); return result; } /** * 返回 key 所储存的字符串值的长度。 * @param key * @return */ public Long strlen(String key) { Jedis jedis = getJedis(); Long result = jedis.strlen(key); releaseJedis(jedis); return result; } /** * 通过key 对value进行加值+1操作,当value不是int类型时会返回错误,当key不存在是则value为1 * @param key * @return 加值后的结果 */ public Long incr(String key) { Jedis jedis = getJedis(); Long result = jedis.incr(key); releaseJedis(jedis); return result; } /** * 对key的值做减减操作,如果key不存在,则设置key为-1 * @param key * @return */ public Long decr(String key) { Jedis jedis = getJedis(); Long result = jedis.decr(key); releaseJedis(jedis); return result; } /** * 减去指定的值 * @param key * @param integer * @return */ public Long decrBy(String key, Integer integer) { Jedis jedis = getJedis(); Long result = jedis.decrBy(key, integer); releaseJedis(jedis); return result; } /** * 通过key向指定的value值追加值 * @param key * @param str * @return 成功返回 添加后value的长度 失败 返回 添加的 value 的长度 异常返回0L */ public Long append(String key, String str) { Jedis jedis = null; Long res = null; try { jedis = getJedis(); res = jedis.append(key, str); } catch (Exception e) { releaseBrokenJedis(jedis); e.printStackTrace(); res = 0L; } finally { releaseJedis(jedis); } return res; } public String subStr(String key, int Start, int end) { Jedis jedis = getJedis(); String result = jedis.substr(key, Start, end); releaseJedis(jedis); return result; } /** * 设置key value并制定这个键值的有效期 * @param key * @param value * @param seconds * 单位:秒 * @return 成功返回OK 失败和异常返回null */ public String setex(String key, String value, int seconds) { Jedis jedis = null; String res = null; try { jedis = getJedis(); res = jedis.setex(key, seconds, value); } catch (Exception e) { releaseBrokenJedis(jedis); e.printStackTrace(); } finally { releaseJedis(jedis); } return res; } /** * 用 value 参数覆写(overwrite)给定 key 所储存的字符串值,从偏移量 offset 开始。 * @param key * @param offset * @param value * @return */ public Long setRange(String key, long offset, String value) { Jedis jedis = getJedis(); Long result = jedis.setrange(key, offset, value); releaseJedis(jedis); return result; } public String getRange(String key, long StartOffset, long endOffset) { Jedis jedis = getJedis(); String result = jedis.getrange(key, StartOffset, endOffset); releaseJedis(jedis); return result; } /** * 查找所有符合给定模式 pattern 的 key 。 * @param key * @return */ public Set keys(String key){ Jedis jedis = getJedis(); Set keys = jedis.keys(key); releaseJedis(jedis); return keys; } public List sort(String key,SortingParams params){ Jedis jedis = getJedis(); List sortedResult = jedis.sort(key,params); releaseJedis(jedis); return sortedResult; } /** * 检测给定key的剩余生存时间,单位秒 * @param key * @return returns -2 if the key does not exist.returns -1 if the key exists but has no associated expire. */ public long ttl(String key){ Long result = -1L; Jedis jedis = null; try{ jedis = getJedis(); result = jedis.ttl(key); }catch (Exception e){ if(jedis != null){ redisPool.returnBrokenResource(jedis); } }finally { redisPool.returnResource(jedis); } return result; } /** * 检测给定key的剩余生存时间,单位毫秒 * @param key * @return returns -2 if the key does not exist.returns -1 if the key exists but has no associated expire. */ public long pttl(String key){ long result = -2L; Jedis jedis = null; try{ jedis = getJedis(); result = jedis.pttl(key); }catch (Exception e){ if(jedis != null){ redisPool.returnBrokenResource(jedis); } }finally { redisPool.returnResource(jedis); } return result; } public RedisPool getRedisPool() { return redisPool; } public void setRedisPool(RedisPool redisPool) { this.redisPool = redisPool; } /** * SCAN 命令用于迭代当前数据库中的数据库键。 * SSCAN 命令用于迭代集合键中的元素。 * HSCAN 命令用于迭代哈希键中的键值对。 *ZSCAN 命令用于迭代有序集合中的元素(包括元素成员和元素分值) * @param key * @return */ }

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
  • 275
  • 276
  • 277
  • 278
  • 279
  • 280
  • 281
  • 282
  • 283
  • 284
  • 285
  • 286
  • 287
  • 288
  • 289
  • 290
  • 291
  • 292
  • 293
  • 294
  • 295
  • 296
  • 297
  • 298
  • 299
  • 300
  • 301
  • 302
  • 303
  • 304
  • 305
  • 306
  • 307
  • 308
  • 309
  • 310
  • 311
  • 312
  • 313
  • 314
  • 315
  • 316
  • 317
  • 318
  • 319
  • 320
  • 321
  • 322
  • 323
  • 324
  • 325
  • 326
  • 327
  • 328
  • 329
  • 330
  • 331
  • 332
  • 333
  • 334
  • 335
  • 336
  • 337
  • 338
  • 339
  • 340
  • 341
  • 342
  • 343
  • 344
  • 345
  • 346
  • 347
  • 348
  • 349
  • 350
  • 351
  • 352
  • 353
  • 354
  • 355
  • 356
  • 357
  • 358
  • 359
  • 360
  • 361
  • 362
  • 363
  • 364
  • 365
  • 366
  • 367
  • 368
  • 369
  • 370
  • 371
  • 372
  • 373
  • 374
  • 375
  • 376
  • 377
  • 378
  • 379
  • 380
  • 381
  • 382
  • 383
  • 384
  • 385
  • 386
  • 387
  • 388
  • 389
  • 390
  • 391
  • 392
  • 393
  • 394
  • 395
  • 396
  • 397
  • 398
  • 399
  • 400
  • 401
  • 402
  • 403
  • 404
  • 405
  • 406
  • 407
  • 408
  • 409
  • 410
  • 411
  • 412
  • 413
  • 414
  • 415
  • 416
  • 417
  • 418
  • 419
  • 420
  • 421
  • 422
  • 423
  • 424
  • 425
  • 426
  • 427
  • 428
  • 429
  • 430
  • 431
  • 432
  • 433
  • 434
  • 435
  • 436
  • 437
  • 438
  • 439
  • 440
  • 441
  • 442
  • 443
  • 444
  • 445
  • 446
  • 447
  • 448
  • 449
  • 450
  • 451
  • 452
  • 453
  • 454
  • 455
  • 456
  • 457
  • 458
  • 459
  • 460
  • 461
  • 462
  • 463
  • 464
  • 465
  • 466
  • 467
  • 468
  • 469
  • 470
  • 471
  • 472
  • 473
  • 474
  • 475
  • 476
  • 477
  • 478
  • 479
  • 480
  • 481
  • 482
  • 483
  • 484
  • 485
  • 486
  • 487
  • 488
  • 489
  • 490
  • 491
  • 492
  • 493
  • 494
  • 495
  • 496
  • 497
  • 498
  • 499
  • 500
  • 501
  • 502
  • 503
  • 504
  • 505
  • 506
  • 507
  • 508
  • 509
  • 510
  • 511
  • 512
  • 513
  • 514
  • 515
  • 516
  • 517
  • 518
  • 519
  • 520
  • 521
  • 522
  • 523
  • 524
  • 525
  • 526
  • 527
  • 528
  • 529
  • 530
  • 531
  • 532
  • 533
  • 534
  • 535
  • 536
  • 537
  • 538
  • 539
  • 540
  • 541
  • 542
  • 543
  • 544
  • 545
  • 546
  • 547
  • 548
  • 549
  • 550
  • 551
  • 552
  • 553
  • 554
  • 555
  • 556
  • 557
  • 558
  • 559
  • 560
  • 561
  • 562
  • 563
  • 564
  • 565
  • 566
  • 567
  • 568
  • 569
  • 570
  • 571
  • 572
  • 573
  • 574
  • 575
  • 576
  • 577
  • 578
  • 579
  • 580
  • 581
  • 582
  • 583
  • 584
  • 585
  • 586
  • 587
  • 588
  • 589
  • 590
  • 591
  • 592
  • 593
  • 594
  • 595
  • 596
  • 597
  • 598
  • 599
  • 600
  • 601
  • 602
  • 603
  • 604
  • 605
  • 606
  • 607
  • 608
  • 609
  • 610
  • 611
  • 612
  • 613
  • 614
  • 615
  • 616
  • 617
  • 618
  • 619
  • 620
  • 621
  • 622
  • 623
  • 624
  • 625
  • 626
  • 627
  • 628
  • 629
  • 630
  • 631
  • 632
  • 633
  • 634
  • 635
  • 636
  • 637
  • 638
  • 639
  • 640
  • 641
  • 642
  • 643
  • 644
  • 645
  • 646
  • 647
  • 648
  • 649
  • 650
  • 651
  • 652
  • 653
  • 654
  • 655
  • 656
  • 657
  • 658
  • 659
  • 660
  • 661
  • 662
  • 663
  • 664
  • 665
  • 666
  • 667
  • 668
  • 669
  • 670
  • 671
  • 672
  • 673
  • 674
  • 675
  • 676
  • 677
  • 678
  • 679
  • 680
  • 681
  • 682
  • 683
  • 684
  • 685
  • 686
  • 687
  • 688
  • 689
  • 690
  • 691
  • 692
  • 693
  • 694
  • 695
  • 696
  • 697
  • 698
  • 699
  • 700
  • 701
  • 702
  • 703
  • 704
  • 705
  • 706
  • 707
  • 708
  • 709
  • 710
  • 711
  • 712
  • 713
  • 714
  • 715
  • 716
  • 717
  • 718
  • 719
  • 720
  • 721
  • 722
  • 723
  • 724
  • 725
  • 726
  • 727
  • 728
  • 729
  • 730
  • 731
  • 732
  • 733
  • 734
  • 735
  • 736
  • 737
  • 738
  • 739
  • 740
  • 741
  • 742
  • 743
  • 744
  • 745
  • 746
  • 747
  • 748
  • 749
  • 750
  • 751
  • 752
  • 753
  • 754
  • 755
  • 756
  • 757
  • 758
  • 759
  • 760
  • 761
  • 762
  • 763
  • 764
  • 765
  • 766
  • 767
  • 768
  • 769
  • 770
  • 771
  • 772
  • 773
  • 774
  • 775
  • 776
  • 777
  • 778
  • 779
  • 780
  • 781
  • 782
  • 783
  • 784
  • 785
  • 786
  • 787
  • 788
  • 789
  • 790
  • 791
  • 792
  • 793
  • 794
  • 795
  • 796
  • 797
  • 798
  • 799
  • 800
  • 801
  • 802
  • 803
  • 804
  • 805
  • 806
  • 807
  • 808
  • 809
  • 810
  • 811
  • 812
  • 813
  • 814
  • 815
  • 816
  • 817
  • 818
  • 819
  • 820
  • 821
  • 822
  • 823
  • 824
  • 825
  • 826
  • 827
  • 828
  • 829
  • 830
  • 831
  • 832
  • 833
  • 834
  • 835
  • 836
  • 837
  • 838
  • 839
  • 840
  • 841
  • 842
  • 843
  • 844
  • 845
  • 846
  • 847
  • 848
  • 849
  • 850
  • 851
  • 852
  • 853
  • 854
  • 855
  • 856
  • 857
  • 858
  • 859
  • 860
  • 861
  • 862
  • 863
  • 864
  • 865
  • 866
  • 867
  • 868
  • 869
  • 870
  • 871
  • 872
  • 873
  • 874
  • 875
  • 876
  • 877
  • 878
  • 879
  • 880
  • 881
  • 882
  • 883
  • 884
  • 885
  • 886
  • 887
  • 888
  • 889
  • 890
  • 891
  • 892
  • 893
  • 894
  • 895
  • 896
  • 897
  • 898
  • 899
  • 900
  • 901
  • 902
  • 903
  • 904
  • 905
  • 906
  • 907
  • 908
  • 909
  • 910
  • 911
  • 912
  • 913
  • 914
  • 915
  • 916
  • 917
  • 918
  • 919
  • 920
  • 921
  • 922
  • 923
  • 924
  • 925
  • 926
  • 927
  • 928
  • 929
  • 930
  • 931
  • 932
  • 933
  • 934
  • 935
  • 936
  • 937
  • 938
  • 939
  • 940
  • 941
  • 942
  • 943
  • 944
  • 945
  • 946
  • 947
  • 948
  • 949
  • 950
  • 951
  • 952
  • 953
  • 954
  • 955
  • 956
  • 957
  • 958
  • 959
  • 960
  • 961
  • 962
  • 963
  • 964
  • 965
  • 966
  • 967
  • 968
  • 969
  • 970
  • 971
  • 972
  • 973
  • 974
  • 975
  • 976
  • 977
  • 978
  • 979
  • 980
  • 981
  • 982
  • 983
  • 984
  • 985
  • 986
  • 987
  • 988
  • 989
  • 990
  • 991
  • 992
  • 993
  • 994
  • 995
  • 996
  • 997
  • 998
  • 999
  • 1000
  • 1001
  • 1002
  • 1003
  • 1004
  • 1005
  • 1006
  • 1007
  • 1008
  • 1009
  • 1010
  • 1011
  • 1012
  • 1013
  • 1014
  • 1015
  • 1016
  • 1017
  • 1018
  • 1019
  • 1020
  • 1021
  • 1022
  • 1023
  • 1024
  • 1025
  • 1026
  • 1027
  • 1028
  • 1029
  • 1030
  • 1031
  • 1032
  • 1033
  • 1034
  • 1035
  • 1036
  • 1037
  • 1038
  • 1039
  • 1040
  • 1041
  • 1042
  • 1043
  • 1044
  • 1045
  • 1046
  • 1047
  • 1048
  • 1049
  • 1050
  • 1051
  • 1052
  • 1053
  • 1054
  • 1055
  • 1056
  • 1057
  • 1058
  • 1059
  • 1060
  • 1061
  • 1062
  • 1063
  • 1064
  • 1065
  • 1066
  • 1067
  • 1068
  • 1069
  • 1070
  • 1071
  • 1072
  • 1073
  • 1074
  • 1075
  • 1076
  • 1077
  • 1078
  • 1079
  • 1080
  • 1081
  • 1082
  • 1083
  • 1084
  • 1085
  • 1086
  • 1087
  • 1088
  • 1089
  • 1090
  • 1091
  • 1092
  • 1093
  • 1094
  • 1095
  • 1096
  • 1097
  • 1098
  • 1099
  • 1100
  • 1101
  • 1102
  • 1103
  • 1104
  • 1105
  • 1106
  • 1107
  • 1108
  • 1109
  • 1110
  • 1111
  • 1112
  • 1113
  • 1114
  • 1115
  • 1116
  • 1117
  • 1118
  • 1119
  • 1120
  • 1121
  • 1122
  • 1123
  • 1124
  • 1125
  • 1126
  • 1127
  • 1128
  • 1129
  • 1130
  • 1131
  • 1132
  • 1133
  • 1134
  • 1135
  • 1136
  • 1137
  • 1138
  • 1139
  • 1140
  • 1141
  • 1142
  • 1143
  • 1144
  • 1145
  • 1146
  • 1147
  • 1148
  • 1149
  • 1150
  • 1151
  • 1152
  • 1153
  • 1154
  • 1155
  • 1156
  • 1157
  • 1158
  • 1159
  • 1160
  • 1161
  • 1162
  • 1163
  • 1164
  • 1165
  • 1166
  • 1167
  • 1168
  • 1169
  • 1170
  • 1171
  • 1172
  • 1173
  • 1174
  • 1175
  • 1176
  • 1177
  • 1178
  • 1179
  • 1180
  • 1181
  • 1182
  • 1183
  • 1184
  • 1185
  • 1186
  • 1187
  • 1188
  • 1189
  • 1190
  • 1191
  • 1192
  • 1193
  • 1194
  • 1195
  • 1196
  • 1197

使用该RedisManager类:

package com.test.cache.redis;
import java.util.HashMap;
import java.util.Map;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import redis.clients.jedis.Jedis;

public class Test {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("spring.xml");
        RedisManager redisManager=(RedisManager)ctx.getBean("redisManager");
        redisManager.flushDB();
        redisManager.set("string_test", "string_test");
        System.out.println("===string类型====:"+redisManager.get("string_test"));

        redisManager.sadd("set_test", "set_1");
        redisManager.sadd("set_test", "set_2");
        redisManager.sadd("set_test", "set_3");
        System.out.println("===set类型====:"+redisManager.smembers("set_test"));

        redisManager.hset("Hash_test", "name", "kxl");
        redisManager.hset("Hash_test", "age", "25");
        redisManager.hset("Hash_test", "sex", "male");
        System.out.println("===hash类型====:"+redisManager.hget("Hash_test", "name"));

        Mapmaps=new HashMap();
        maps.put("name", "xiaohong");
        maps.put("age", "20");
        maps.put("sex", "female");
        redisManager.hmset("Hash_test2", maps);
        System.out.println("===hash类型测试2====:"+redisManager.hmget("Hash_test2","name","age"));

        redisManager.lpush("List", "3");
        redisManager.lpush("List", "7");
        redisManager.lpush("List", "1");
        System.out.println("===List类型====:"+redisManager.lrange("List", 0, -1));

        redisManager.zadd("sorted set", 10, "set1");
        redisManager.zadd("sorted set", 2, "set2");
        redisManager.zadd("sorted set", 3, "set3");
        System.out.println("==sorted sett类型====:"+redisManager.zrevrangeByScore("sorted set", 10, 0));

    }
}
   
   
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43

测试结果:

===string类型====:string_test
===set类型====:[set_3, set_2, set_1]
===hash类型====:kxl
===hash类型测试2====:[xiaohong, 20]
===List类型====:[1, 7, 3]
==sorted sett类型====:[set1, set3, set2]
   
   
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
					
            
阅读更多

你可能感兴趣的:(Redis+Jedis+Spring封装RedisManager类操作缓存)