首页

通过spring的FactoryBean注册JedisPool池来实现常用Redis操作工具类RedisUtils

标签:jedis,客户端,redis,分布式,kw,工具包,内存,spring,JedisPoolFactoryBean,连接池     发布时间:2016-10-10   

1. 通过Redis官方提供Jedis客户端开发包(jedis-2.6.0.jar),对于不同的数据结构类型存取处理差异性比较大,针对性处理复杂性比较大,这里通过RedisUtil工具类相似化了各个数据类型的操作,从而简化了对于Redis使用的难度,降低了代码结构的复杂度,具体代码示例如下

import java.util.List;@b@import java.util.Map;@b@import java.util.Set;@b@import redis.clients.jedis.Jedis;@b@import redis.clients.jedis.JedisPool;@b@import redis.clients.jedis.Pipeline;@b@import redis.clients.jedis.Response;@b@import redis.clients.jedis.Transaction;@b@import redis.clients.jedis.Tuple;@b@@b@public class RedisUtils {@b@@b@    private JedisPool pool;@b@@b@    /**@b@     * Set the string value as value of the key. The string can't be longer than 1073741824 bytes (1@b@     * GB). Time complexity: O(1)@b@     *@b@     * @param key the key@b@     * @param value the value@b@     * @return string@b@     */@b@    public String setString(final String key, final String value) {@b@        return new JedisPoolOperationTemplate(pool).doOperation(new Operation<String>() {@b@@b@            public String operate(Jedis jedis) {@b@                return jedis.set(key, value);@b@            }@b@        });@b@    }@b@@b@    @b@    public String set(final byte[] key, final byte[] value) {@b@        return new JedisPoolOperationTemplate(pool).doOperation(new Operation<String>() {@b@@b@            public String operate(Jedis jedis) {@b@                return jedis.set(key, value);@b@            }@b@        });@b@    }@b@@b@ @b@    public String set(String key, Object value) {@b@        return set(key.getBytes(), SerializeUtils.serializeObject(value));@b@    }@b@@b@  @b@    public Long set(String key, Object value, int seconds) {@b@        set(key.getBytes(), SerializeUtils.serializeObject(value));@b@        return expire(key, seconds);@b@    }@b@@b@    @b@    public byte[] get(final byte[] key) {@b@        return new JedisPoolOperationTemplate(pool).doOperation(new Operation<byte[]>() {@b@            public byte[] operate(Jedis jedis) {@b@                return jedis.get(key);@b@            }@b@        });@b@@b@    }@b@@b@    @b@    public Object get(String key) {@b@        return SerializeUtils.deserializeObject(get(key.getBytes()));@b@@b@    }@b@@b@    @b@    public Boolean exists(final String key) {@b@        return new JedisPoolOperationTemplate(pool).doOperation(new Operation<Boolean>() {@b@            public Boolean operate(Jedis jedis) {@b@                return jedis.exists(key);@b@            }@b@        });@b@    }@b@@b@@b@     @b@    public Boolean exists(final byte[] key) {@b@        return new JedisPoolOperationTemplate(pool).doOperation(new Operation<Boolean>() {@b@            public Boolean operate(Jedis jedis) {@b@                return jedis.exists(key);@b@            }@b@        });@b@    }@b@@b@     @b@    public Long expire(final String key, final int seconds) {@b@        return new JedisPoolOperationTemplate(pool).doOperation(new Operation<Long>() {@b@@b@            public Long operate(Jedis jedis) {@b@                return jedis.expire(key, seconds);@b@            }@b@        });@b@    }@b@@b@     @b@    public Long del(final String key) {@b@        return new JedisPoolOperationTemplate(pool).doOperation(new Operation<Long>() {@b@@b@            public Long operate(Jedis jedis) {@b@                return jedis.del(key);@b@            }@b@        });@b@    }@b@@b@     @b@    public Long del(final byte[] key) {@b@        return new JedisPoolOperationTemplate(pool).doOperation(new Operation<Long>() {@b@@b@            public Long operate(Jedis jedis) {@b@                return jedis.del(key);@b@            }@b@        });@b@    }@b@@b@     @b@    public String rename(final String oldkey, final String newkey) {@b@        return new JedisPoolOperationTemplate(pool).doOperation(new Operation<String>() {@b@@b@            public String operate(Jedis jedis) {@b@                return jedis.rename(oldkey, newkey);@b@            }@b@        });@b@    }@b@@b@     @b@    public Long renamenx(final String oldkey, final String newkey) {@b@        return new JedisPoolOperationTemplate(pool).doOperation(new Operation<Long>() {@b@@b@            public Long operate(Jedis jedis) {@b@                return jedis.renamenx(oldkey, newkey);@b@            }@b@        });@b@    }@b@@b@   @b@    public long dbsize() {@b@        return new JedisPoolOperationTemplate(pool).doOperation(new Operation<Long>() {@b@@b@            public Long operate(Jedis jedis) {@b@                return jedis.dbSize();@b@            }@b@        });@b@    }@b@@b@    @b@    public String type(final String key) {@b@        return new JedisPoolOperationTemplate(pool).doOperation(new Operation<String>() {@b@@b@            public String operate(Jedis jedis) {@b@                return jedis.type(key);@b@            }@b@        });@b@@b@    }@b@@b@     @b@    public Long sadd(final String key, final String... members) {@b@        return new JedisPoolOperationTemplate(pool).doOperation(new Operation<Long>() {@b@@b@            public Long operate(Jedis jedis) {@b@                return jedis.sadd(key, members);@b@            }@b@        });@b@    }@b@@b@     @b@    public Long sadd(final byte[] key, final byte[]... members) {@b@        return new JedisPoolOperationTemplate(pool).doOperation(new Operation<Long>() {@b@@b@            public Long operate(Jedis jedis) {@b@                return jedis.sadd(key, members);@b@            }@b@        });@b@    }@b@@b@    @b@    public Long srem(final String key, final String... members) {@b@        return new JedisPoolOperationTemplate(pool).doOperation(new Operation<Long>() {@b@@b@            public Long operate(Jedis jedis) {@b@                return jedis.srem(key, members);@b@            }@b@        });@b@    }@b@@b@     @b@    public Long srem(final byte[] key, final byte[]... members) {@b@        return new JedisPoolOperationTemplate(pool).doOperation(new Operation<Long>() {@b@@b@            public Long operate(Jedis jedis) {@b@                return jedis.srem(key, members);@b@            }@b@        });@b@    }@b@@b@     @b@    public Long scard(final String key) {@b@        return new JedisPoolOperationTemplate(pool).doOperation(new Operation<Long>() {@b@@b@            public Long operate(Jedis jedis) {@b@                return jedis.scard(key);@b@            }@b@        });@b@    }@b@@b@    @b@    public Set<String> smembers(final String key) {@b@        return new JedisPoolOperationTemplate(pool).doOperation(new Operation<Set<String>>() {@b@@b@            public Set<String> operate(Jedis jedis) {@b@                return jedis.smembers(key);@b@            }@b@        });@b@    }@b@@b@     @b@    public Boolean sismember(final String key, final String member) {@b@        return new JedisPoolOperationTemplate(pool).doOperation(new Operation<Boolean>() {@b@@b@            public Boolean operate(Jedis jedis) {@b@                return jedis.sismember(key, member);@b@            }@b@        });@b@    }@b@@b@    @b@    public Set<String> sinter(final String... keys) {@b@        return new JedisPoolOperationTemplate(pool).doOperation(new Operation<Set<String>>() {@b@@b@            public Set<String> operate(Jedis jedis) {@b@                return jedis.sinter(keys);@b@            }@b@        });@b@@b@    }@b@@b@  @b@    public Set<String> sunion(final String... keys) {@b@        return new JedisPoolOperationTemplate(pool).doOperation(new Operation<Set<String>>() {@b@@b@            public Set<String> operate(Jedis jedis) {@b@                return jedis.sunion(keys);@b@            }@b@        });@b@@b@    }@b@@b@    @b@    public Set<String> sdiff(final String... keys) {@b@        return new JedisPoolOperationTemplate(pool).doOperation(new Operation<Set<String>>() {@b@@b@            public Set<String> operate(Jedis jedis) {@b@                return jedis.sdiff(keys);@b@            }@b@        });@b@@b@    }@b@ @b@    public Set<String> keys(final String pattern) {@b@        return new JedisPoolOperationTemplate(pool).doOperation(new Operation<Set<String>>() {@b@@b@            public Set<String> operate(Jedis jedis) {@b@                return jedis.keys(pattern);@b@            }@b@        });@b@@b@    }@b@@b@   @b@    public Long expireAt(final String key, final long unixTime) {@b@        return new JedisPoolOperationTemplate(pool).doOperation(new Operation<Long>() {@b@@b@            public Long operate(Jedis jedis) {@b@                return jedis.expireAt(key, unixTime);@b@            }@b@        });@b@    }@b@@b@   @b@    public Long ttl(final String key) {@b@        return new JedisPoolOperationTemplate(pool).doOperation(new Operation<Long>() {@b@@b@            public Long operate(Jedis jedis) {@b@                return jedis.ttl(key);@b@            }@b@        });@b@    }@b@@b@    /**@b@     * @b@     * @param key@b@     * @param value@b@     * @return@b@     */@b@    public Long setnx(final String key, final String value) {@b@        return new JedisPoolOperationTemplate(pool).doOperation(new Operation<Long>() {@b@@b@            public Long operate(Jedis jedis) {@b@                return jedis.setnx(key, value);@b@            }@b@        });@b@    }@b@@b@    /**@b@     * @b@     * @param keys@b@     * @return@b@     */@b@    public List<String> mget(final String... keys) {@b@        return new JedisPoolOperationTemplate(pool).doOperation(new Operation<List<String>>() {@b@@b@            public List<String> operate(Jedis jedis) {@b@                return jedis.mget(keys);@b@            }@b@        });@b@    }@b@@b@    /**@b@     * @b@     * @param keysvalues@b@     * @return@b@     */@b@    public String mset(final String... keysvalues) {@b@        return new JedisPoolOperationTemplate(pool).doOperation(new Operation<String>() {@b@@b@            public String operate(Jedis jedis) {@b@                return jedis.mset(keysvalues);@b@            }@b@        });@b@@b@    }@b@@b@    /**@b@     * @b@     * @param keys@b@     * @return@b@     */@b@    public Long msetnx(final String... keys) {@b@        return new JedisPoolOperationTemplate(pool).doOperation(new Operation<Long>() {@b@@b@            public Long operate(Jedis jedis) {@b@                return jedis.msetnx(keys);@b@            }@b@        });@b@    }@b@@b@    /**@b@     * @b@     * @param key@b@     * @param field@b@     * @param value@b@     * @return@b@     */@b@    public Long hset(final String key, final String field, final String value) {@b@        return new JedisPoolOperationTemplate(pool).doOperation(new Operation<Long>() {@b@@b@            public Long operate(Jedis jedis) {@b@                return jedis.hset(key, field, value);@b@            }@b@        });@b@    }@b@@b@    /**@b@     * Hset long.@b@     *@b@     * @param key the key@b@     * @param field the field@b@     * @param value the value@b@     * @return the long@b@     */@b@    public Long hset(final byte[] key, final byte[] field, final byte[] value) {@b@        return new JedisPoolOperationTemplate(pool).doOperation(new Operation<Long>() {@b@@b@            public Long operate(Jedis jedis) {@b@                return jedis.hset(key, field, value);@b@            }@b@        });@b@    }@b@@b@    public Response<Long> hsetpipe(final byte[] key, final byte[] field, final byte[] value) {@b@        return new JedisPoolOperationTemplate(pool).doOperation(new Operation<Response<Long>>() {@b@@b@            public Response<Long> operate(Jedis jedis) {@b@                Pipeline pipeline = jedis.pipelined();@b@                return pipeline.hset(key, field, value);@b@            }@b@        });@b@    }@b@@b@    /**@b@     * @b@     * @param key@b@     * @param field@b@     * @param value@b@     * @return@b@     */@b@    public Long hsetnx(final String key, final String field, final String value) {@b@        return new JedisPoolOperationTemplate(pool).doOperation(new Operation<Long>() {@b@@b@            public Long operate(Jedis jedis) {@b@                return jedis.hsetnx(key, field, value);@b@            }@b@        });@b@    }@b@@b@    /**@b@     * @b@     * @param key@b@     * @param field@b@     * @param value@b@     * @return@b@     */@b@    public Long hsetnx(final byte[] key, final byte[] field, final byte[] value) {@b@        return new JedisPoolOperationTemplate(pool).doOperation(new Operation<Long>() {@b@@b@            public Long operate(Jedis jedis) {@b@                return jedis.hsetnx(key, field, value);@b@            }@b@        });@b@    }@b@@b@    /**@b@     * @b@     * @param key@b@     * @param hash@b@     * @return@b@     */@b@    public String hmset(final String key, final Map<String, String> hash) {@b@        return new JedisPoolOperationTemplate(pool).doOperation(new Operation<String>() {@b@@b@            public String operate(Jedis jedis) {@b@                return jedis.hmset(key, hash);@b@            }@b@        });@b@    }@b@@b@    public String hmset(final byte[] key, final Map<byte[], byte[]> hash) {@b@        return new JedisPoolOperationTemplate(pool).doOperation(new Operation<String>() {@b@@b@            public String operate(Jedis jedis) {@b@                return jedis.hmset(key, hash);@b@            }@b@        });@b@    }@b@@b@    /**@b@     * @b@     * @param key@b@     * @param field@b@     * @return@b@     */@b@    public String hget(final String key, final String field) {@b@        return new JedisPoolOperationTemplate(pool).doOperation(new Operation<String>() {@b@@b@            public String operate(Jedis jedis) {@b@                return jedis.hget(key, field);@b@            }@b@        });@b@    }@b@@b@    /**@b@     * Hget byte [ ].@b@     *@b@     * @param key the key@b@     * @param field the field@b@     * @return the byte [ ]@b@     */@b@    public byte[] hget(final byte[] key, final byte[] field) {@b@        return new JedisPoolOperationTemplate(pool).doOperation(new Operation<byte[]>() {@b@@b@            public byte[] operate(Jedis jedis) {@b@                return jedis.hget(key, field);@b@            }@b@        });@b@    }@b@@b@    /**@b@     * @b@     * @param key@b@     * @param fields@b@     * @return@b@     */@b@    public List<String> hmget(final String key, final String... fields) {@b@        return new JedisPoolOperationTemplate(pool).doOperation(new Operation<List<String>>() {@b@@b@            public List<String> operate(Jedis jedis) {@b@                return jedis.hmget(key, fields);@b@            }@b@        });@b@    }@b@@b@    /**@b@     * @b@     * @param key@b@     * @return@b@     */@b@    public Map<String, String> hgetall(final String key) {@b@        return new JedisPoolOperationTemplate(pool).doOperation(new Operation<Map<String, String>>() {@b@@b@            public Map<String, String> operate(Jedis jedis) {@b@                return jedis.hgetAll(key);@b@            }@b@        });@b@    }@b@@b@    /**@b@     * Hgetall map.@b@     *@b@     * @param key the key@b@     * @return the map@b@     */@b@    public Map<byte[], byte[]> hgetall(final byte[] key) {@b@        return new JedisPoolOperationTemplate(pool).doOperation(new Operation<Map<byte[], byte[]>>() {@b@@b@            public Map<byte[], byte[]> operate(Jedis jedis) {@b@                return jedis.hgetAll(key);@b@            }@b@        });@b@    }@b@@b@    /**@b@     * @b@     * @param key@b@     * @param fields@b@     * @return@b@     */@b@    public Long hdel(final String key, final String... fields) {@b@        return new JedisPoolOperationTemplate(pool).doOperation(new Operation<Long>() {@b@@b@            public Long operate(Jedis jedis) {@b@                return jedis.hdel(key, fields);@b@            }@b@        });@b@    }@b@@b@    /**@b@     * Hdel long.@b@     *@b@     * @param key the key@b@     * @param fields the fields@b@     * @return the long@b@     */@b@    public Long hdel(final byte[] key, final byte[]... fields) {@b@        return new JedisPoolOperationTemplate(pool).doOperation(new Operation<Long>() {@b@@b@            public Long operate(Jedis jedis) {@b@                return jedis.hdel(key, fields);@b@            }@b@        });@b@    }@b@@b@    /**@b@     * @b@     * @param key@b@     * @return@b@     */@b@    public Long hlen(final String key) {@b@        return new JedisPoolOperationTemplate(pool).doOperation(new Operation<Long>() {@b@@b@            public Long operate(Jedis jedis) {@b@                return jedis.hlen(key);@b@            }@b@        });@b@    }@b@@b@    /**@b@     * @b@     * @param key@b@     * @param field@b@     * @return@b@     */@b@    public boolean hexists(final String key, final String field) {@b@        return new JedisPoolOperationTemplate(pool).doOperation(new Operation<Boolean>() {@b@@b@            public Boolean operate(Jedis jedis) {@b@                return jedis.hexists(key, field);@b@            }@b@        });@b@    }@b@@b@    /**@b@     * @b@     * @param key@b@     * @param field@b@     * @param value@b@     * @return@b@     */@b@    public Long hincrby(final String key, final String field, final long value) {@b@        return new JedisPoolOperationTemplate(pool).doOperation(new Operation<Long>() {@b@@b@            public Long operate(Jedis jedis) {@b@                return jedis.hincrBy(key, field, value);@b@            }@b@        });@b@    }@b@@b@    /**@b@     * Hincrbyfloat double.@b@     *@b@     * @param key the key@b@     * @param field the field@b@     * @param value the value@b@     * @return the double@b@     */@b@    public Double hincrbyfloat(final String key, final String field, final long value) {@b@        return new JedisPoolOperationTemplate(pool).doOperation(new Operation<Double>() {@b@@b@            public Double operate(Jedis jedis) {@b@                return jedis.hincrByFloat(key, field, value);@b@            }@b@        });@b@@b@    }@b@@b@    /**@b@     * @b@     * @param key@b@     * @return@b@     */@b@    public Set<String> hkeys(final String key) {@b@        return new JedisPoolOperationTemplate(pool).doOperation(new Operation<Set<String>>() {@b@@b@            public Set<String> operate(Jedis jedis) {@b@                return jedis.hkeys(key);@b@            }@b@        });@b@@b@    }@b@@b@    /**@b@     * @b@     * @param key@b@     * @return@b@     */@b@    public List<String> hvals(final String key) {@b@        return new JedisPoolOperationTemplate(pool).doOperation(new Operation<List<String>>() {@b@@b@            public List<String> operate(Jedis jedis) {@b@                return jedis.hvals(key);@b@            }@b@        });@b@    }@b@@b@    /**@b@     * @b@     * @param key@b@     * @param seconds@b@     * @param value@b@     * @return@b@     */@b@    public String setex(final String key, final int seconds, final String value) {@b@        return new JedisPoolOperationTemplate(pool).doOperation(new Operation<String>() {@b@@b@            public String operate(Jedis jedis) {@b@                return jedis.setex(key, seconds, value);@b@            }@b@        });@b@    }@b@@b@    /**@b@     * @b@     * @param key@b@     * @return@b@     */@b@    public Long incr(final String key) {@b@        return new JedisPoolOperationTemplate(pool).doOperation(new Operation<Long>() {@b@@b@            public Long operate(Jedis jedis) {@b@                return jedis.incr(key);@b@            }@b@        });@b@@b@    }@b@@b@    /**@b@     * Incr by.@b@     *@b@     * @param key the key@b@     * @param step the step@b@     * @return the long@b@     */@b@    public Long incrBy(final String key, final Long step) {@b@        return new JedisPoolOperationTemplate(pool).doOperation(new Operation<Long>() {@b@@b@            public Long operate(Jedis jedis) {@b@                return jedis.incrBy(key, step);@b@            }@b@        });@b@@b@    }@b@@b@    /**@b@     * Incr long.@b@     *@b@     * @param key the key@b@     * @return the long@b@     */@b@    public Long incr(final byte[] key) {@b@        return new JedisPoolOperationTemplate(pool).doOperation(new Operation<Long>() {@b@@b@            public Long operate(Jedis jedis) {@b@                return jedis.incr(key);@b@            }@b@        });@b@@b@    }@b@@b@    /**@b@     * @b@     * @param key@b@     * @param value@b@     * @return@b@     */@b@    public Long append(final String key, final String value) {@b@        return new JedisPoolOperationTemplate(pool).doOperation(new Operation<Long>() {@b@@b@            public Long operate(Jedis jedis) {@b@                return jedis.append(key, value);@b@            }@b@        });@b@    }@b@@b@    /**@b@     * @b@     * @param key@b@     * @param start@b@     * @param end@b@     * @return@b@     */@b@    public String substr(final String key, final int start, final int end) {@b@        return new JedisPoolOperationTemplate(pool).doOperation(new Operation<String>() {@b@@b@            public String operate(Jedis jedis) {@b@                return jedis.substr(key, start, end);@b@            }@b@        });@b@    }@b@@b@    /**@b@     * @b@     * @param key@b@     * @param strings@b@     * @return@b@     */@b@    public Long rpush(final String key, final String... strings) {@b@        return new JedisPoolOperationTemplate(pool).doOperation(new Operation<Long>() {@b@@b@            public Long operate(Jedis jedis) {@b@                return jedis.rpush(key, strings);@b@            }@b@        });@b@    }@b@@b@    /**@b@     * @b@     * @param key@b@     * @param bytes@b@     * @return@b@     */@b@    public Long rpush(final byte[] key, final byte[]... bytes) {@b@        return new JedisPoolOperationTemplate(pool).doOperation(new Operation<Long>() {@b@@b@            public Long operate(Jedis jedis) {@b@                return jedis.rpush(key, bytes);@b@            }@b@        });@b@    }@b@@b@    /**@b@     * @b@     * @param key@b@     * @param strings@b@     * @return@b@     */@b@    public long lpush(final String key, final String... strings) {@b@        return new JedisPoolOperationTemplate(pool).doOperation(new Operation<Long>() {@b@@b@            public Long operate(Jedis jedis) {@b@                return jedis.lpush(key, strings);@b@            }@b@        });@b@    }@b@@b@    public long lpush(final byte[] key, final byte[]... strings) {@b@        return new JedisPoolOperationTemplate(pool).doOperation(new Operation<Long>() {@b@@b@            public Long operate(Jedis jedis) {@b@                return jedis.lpush(key, strings);@b@            }@b@        });@b@    }@b@@b@    /**@b@     * Rpop string.@b@     *@b@     * @param key the key@b@     * @return the string@b@     */@b@    public String rpop(final String key) {@b@        return new JedisPoolOperationTemplate(pool).doOperation(new Operation<String>() {@b@@b@            public String operate(Jedis jedis) {@b@                return jedis.rpop(key);@b@            }@b@        });@b@    }@b@@b@    /**@b@     * Rpoplpush string.@b@     *@b@     * @param source the source@b@     * @param destination the destination@b@     * @return the string@b@     */@b@    public String rpoplpush(final String source, final String destination) {@b@        return new JedisPoolOperationTemplate(pool).doOperation(new Operation<String>() {@b@@b@            public String operate(Jedis jedis) {@b@                return jedis.rpoplpush(source, destination);@b@            }@b@        });@b@    }@b@@b@    /**@b@     * Lpop string.@b@     *@b@     * @param key the key@b@     * @return the string@b@     */@b@    public String lpop(final String key) {@b@        return new JedisPoolOperationTemplate(pool).doOperation(new Operation<String>() {@b@@b@            public String operate(Jedis jedis) {@b@                return jedis.lpop(key);@b@            }@b@        });@b@    }@b@@b@    public byte[] lpop(final byte[] key) {@b@        return new JedisPoolOperationTemplate(pool).doOperation(new Operation<byte[]>() {@b@@b@            public byte[] operate(Jedis jedis) {@b@                return jedis.lpop(key);@b@            }@b@        });@b@    }@b@@b@    /**@b@     * Lpushpipe response.@b@     *@b@     * @param key the key@b@     * @param strings the strings@b@     * @return the response@b@     */@b@    public Response<Long> lpushpipe(final String key, final String... strings) {@b@        return new JedisPoolOperationTemplate(pool).doOperation(new Operation<Response<Long>>() {@b@@b@            public Response<Long> operate(Jedis jedis) {@b@                Pipeline pipeline = jedis.pipelined();@b@@b@                Response<Long> response = pipeline.lpush(key, strings);@b@                pipeline.sync();@b@@b@                return response;@b@            }@b@        });@b@    }@b@@b@    /**@b@     * Rpushpipe response.@b@     *@b@     * @param key the key@b@     * @param strings the strings@b@     * @return the response@b@     */@b@    public Response<Long> rpushpipe(final String key, final String... strings) {@b@        return new JedisPoolOperationTemplate(pool).doOperation(new Operation<Response<Long>>() {@b@@b@            public Response<Long> operate(Jedis jedis) {@b@                Pipeline pipeline = jedis.pipelined();@b@@b@                Response<Long> response = pipeline.rpush(key, strings);@b@                pipeline.sync();@b@@b@                return response;@b@            }@b@        });@b@    }@b@@b@    /**@b@     * Rpushpipe response.@b@     *@b@     * @param key the key@b@     * @param strings the strings@b@     * @return the response@b@     */@b@    public Response<Long> rpushpipe(final byte[] key, final byte[]... strings) {@b@        return new JedisPoolOperationTemplate(pool).doOperation(new Operation<Response<Long>>() {@b@@b@            public Response<Long> operate(Jedis jedis) {@b@                Pipeline pipeline = jedis.pipelined();@b@@b@                Response<Long> response = pipeline.rpush(key, strings);@b@                pipeline.sync();@b@@b@                return response;@b@            }@b@        });@b@    }@b@@b@    /**@b@     * @b@     * @param seconds@b@     * @param key@b@     * @param strings@b@     * @return@b@     */@b@    public Long lpush(int seconds, String key, String... strings) {@b@        lpush(key, strings);@b@        return expire(key, seconds);@b@    }@b@@b@    /**@b@     * @b@     * @param key@b@     * @return@b@     */@b@    public Long llen(final String key) {@b@        return new JedisPoolOperationTemplate(pool).doOperation(new Operation<Long>() {@b@@b@            public Long operate(Jedis jedis) {@b@                return jedis.llen(key);@b@            }@b@        });@b@    }@b@@b@    /**@b@     * @b@     * @param key@b@     * @return@b@     */@b@    public Long llen(final byte[] key) {@b@        return new JedisPoolOperationTemplate(pool).doOperation(new Operation<Long>() {@b@@b@            public Long operate(Jedis jedis) {@b@                return jedis.llen(key);@b@            }@b@        });@b@    }@b@@b@    public List<String> lrange(final String key, final long start, final long end) {@b@        return new JedisPoolOperationTemplate(pool).doOperation(new Operation<List<String>>() {@b@@b@            public List<String> operate(Jedis jedis) {@b@                return jedis.lrange(key, start, end);@b@            }@b@        });@b@    }@b@@b@    public List<byte[]> lrange(final byte[] key, final long start, final long end) {@b@        return new JedisPoolOperationTemplate(pool).doOperation(new Operation<List<byte[]>>() {@b@@b@            public List<byte[]> operate(Jedis jedis) {@b@                return jedis.lrange(key, start, end);@b@            }@b@        });@b@    }@b@@b@    public List<String> lrangetrim(final String key, final long start, final long end) {@b@        return this.executeInTx(new TransactionalOperation<List<String>>() {@b@@b@            public List<String> operation(Transaction tx) {@b@                Response<List<String>> result = tx.lrange(key, start, end);@b@                tx.ltrim(key, end + 1, -1);@b@                tx.exec();@b@                return result.get();@b@            }@b@        });@b@    }@b@@b@    public List<byte[]> lrangetrim(final byte[] key, final long start, final long end) {@b@        return this.executeInTx(new TransactionalOperation<List<byte[]>>() {@b@@b@            public List<byte[]> operation(Transaction tx) {@b@                Response<List<byte[]>> result = tx.lrange(key, start, end);@b@                tx.ltrim(key, end + 1, -1);@b@                tx.exec();@b@                return result.get();@b@            }@b@        });@b@    }@b@@b@    /**@b@     * @b@     * @param key@b@     * @param start@b@     * @param end@b@     * @return@b@     */@b@    public String ltrim(final String key, final long start, final long end) {@b@        return new JedisPoolOperationTemplate(pool).doOperation(new Operation<String>() {@b@@b@            public String operate(Jedis jedis) {@b@                return jedis.ltrim(key, start, end);@b@            }@b@        });@b@    }@b@@b@    public String lindex(final String key, final long index) {@b@        return new JedisPoolOperationTemplate(pool).doOperation(new Operation<String>() {@b@@b@            public String operate(Jedis jedis) {@b@                return jedis.lindex(key, index);@b@            }@b@        });@b@    }@b@@b@    public String lset(final String key, final long index, final String value) {@b@        return new JedisPoolOperationTemplate(pool).doOperation(new Operation<String>() {@b@@b@            public String operate(Jedis jedis) {@b@                return jedis.lset(key, index, value);@b@            }@b@        });@b@    }@b@@b@    public Long lrem(final String key, final long count, final String value) {@b@        return new JedisPoolOperationTemplate(pool).doOperation(new Operation<Long>() {@b@@b@            public Long operate(Jedis jedis) {@b@                return jedis.lrem(key, count, value);@b@            }@b@        });@b@    }@b@@b@    public Long lrem(final byte[] key, final long count, final byte[] value) {@b@        return new JedisPoolOperationTemplate(pool).doOperation(new Operation<Long>() {@b@@b@            public Long operate(Jedis jedis) {@b@                return jedis.lrem(key, count, value);@b@            }@b@        });@b@    }@b@@b@    /**@b@     * @b@     * @return@b@     */@b@    public boolean isconnect() {@b@        return new JedisPoolOperationTemplate(pool).doOperation(new Operation<Boolean>() {@b@@b@            public Boolean operate(Jedis jedis) {@b@                return jedis.isConnected();@b@            }@b@        });@b@    }@b@@b@    /**@b@     * @b@     */@b@    public void disconnect() {@b@        new JedisPoolOperationTemplate(pool).doOperation(new Operation<Object>() {@b@@b@            public Object operate(Jedis jedis) {@b@                jedis.disconnect();@b@                return null;@b@            }@b@        });@b@@b@    }@b@@b@    public String getString(final String key) {@b@        return new JedisPoolOperationTemplate(pool).doOperation(new Operation<String>() {@b@@b@            public String operate(Jedis jedis) {@b@                return jedis.get(key);@b@            }@b@        });@b@    }@b@@b@    public <T> T executeInTx(final TransactionalOperation<T> operation) {@b@        return new JedisPoolOperationTemplate(pool).doOperation(new Operation<T>() {@b@@b@            public T operate(Jedis jedis) {@b@@b@                Transaction tx = jedis.multi();@b@                return operation.operation(tx);@b@            }@b@        });@b@    }@b@@b@    public Map<String, String> hgetAll(final String key) {@b@        return new JedisPoolOperationTemplate(pool).doOperation(new Operation<Map<String, String>>() {@b@@b@            public Map<String, String> operate(Jedis jedis) {@b@                return jedis.hgetAll(key);@b@            }@b@        });@b@    }@b@@b@    public String brpoplpush(final String source, final String destination, final int timeout) {@b@        return new JedisPoolOperationTemplate(pool).doOperation(new Operation<String>() {@b@@b@            public String operate(Jedis jedis) {@b@                return jedis.brpoplpush(source, destination, timeout);@b@            }@b@        });@b@    }@b@@b@    public byte[] brpoplpush(final byte[] source, final byte[] destination, final int timeout) {@b@        return new JedisPoolOperationTemplate(pool).doOperation(new Operation<byte[]>() {@b@@b@            public byte[] operate(Jedis jedis) {@b@                return jedis.brpoplpush(source, destination, timeout);@b@            }@b@        });@b@    }@b@@b@    public Long zadd(final byte[] key, final double score, final byte[] member) {@b@        return new JedisPoolOperationTemplate(pool).doOperation(new Operation<Long>() {@b@@b@            public Long operate(Jedis jedis) {@b@                return jedis.zadd(key, score, member);@b@            }@b@        });@b@    }@b@@b@    public Long zadd(final String key, final double score, final String member) {@b@        return new JedisPoolOperationTemplate(pool).doOperation(new Operation<Long>() {@b@@b@            public Long operate(Jedis jedis) {@b@                return jedis.zadd(key, score, member);@b@            }@b@        });@b@    }@b@@b@    public Set<String> zrangebyscore(final String key, final Double min, final Double max) {@b@        return new JedisPoolOperationTemplate(pool).doOperation(new Operation<Set<String>>() {@b@@b@            public Set<String> operate(Jedis jedis) {@b@                return jedis.zrangeByScore(key, min, max);@b@            }@b@        });@b@    }@b@@b@    public Set<Tuple> zrangebyscorewithscores(final String key, final Double min, final Double max) {@b@        return new JedisPoolOperationTemplate(pool).doOperation(new Operation<Set<Tuple>>() {@b@@b@            public Set<Tuple> operate(Jedis jedis) {@b@                return jedis.zrangeByScoreWithScores(key, min, max);@b@            }@b@        });@b@    }@b@@b@    public Set<String> zrevrangebyscore(final String key, final Double min, final Double max) {@b@        return new JedisPoolOperationTemplate(pool).doOperation(new Operation<Set<String>>() {@b@@b@            public Set<String> operate(Jedis jedis) {@b@                return jedis.zrevrangeByScore(key, min, max);@b@            }@b@        });@b@    }@b@@b@    public Set<Tuple> zrevrangebyscorewithscores(final String key, final Double min, final Double max) {@b@        return new JedisPoolOperationTemplate(pool).doOperation(new Operation<Set<Tuple>>() {@b@@b@            public Set<Tuple> operate(Jedis jedis) {@b@                return jedis.zrevrangeByScoreWithScores(key, min, max);@b@            }@b@        });@b@    }@b@@b@    public Long zremrangebyscore(final String key, final Double min, final Double max) {@b@        return new JedisPoolOperationTemplate(pool).doOperation(new Operation<Long>() {@b@@b@            public Long operate(Jedis jedis) {@b@                return jedis.zremrangeByScore(key, min, max);@b@            }@b@        });@b@    }@b@@b@    public Long zrank(final String key, final String member) {@b@        return new JedisPoolOperationTemplate(pool).doOperation(new Operation<Long>() {@b@@b@            public Long operate(Jedis jedis) {@b@                return jedis.zrank(key, member);@b@            }@b@        });@b@    }@b@@b@    public Long zremrangebyrank(final String key, final Long start, final Long end) {@b@        return new JedisPoolOperationTemplate(pool).doOperation(new Operation<Long>() {@b@@b@            public Long operate(Jedis jedis) {@b@                return jedis.zremrangeByRank(key, start, end);@b@            }@b@        });@b@    }@b@@b@    private static class JedisPoolOperationTemplate {@b@@b@        // private JedisSentinelPool jedisPool;@b@        private JedisPool jedisPool;@b@@b@        /**@b@         * Instantiates a new Jedis pool operation template.@b@         *@b@         * @param jedisPool the jedis pool@b@         */@b@        // public JedisPoolOperationTemplate(JedisSentinelPool jedisPool) {@b@        // this.jedisPool = jedisPool;@b@        // }@b@@b@        public JedisPoolOperationTemplate(JedisPool jedisPool) {@b@            this.jedisPool = jedisPool;@b@        }@b@@b@        /**@b@         * Do operation.@b@         *@b@         * @param operation the operation@b@         * @return the t@b@         */@b@        public <T> T doOperation(Operation<T> operation) {@b@            Jedis jedis = jedisPool.getResource();@b@            try {@b@                return operation.operate(jedis);@b@            } catch (Exception e) {@b@                return null;@b@            } finally {@b@                jedisPool.returnResource(jedis);@b@            }@b@        }@b@    }@b@@b@    /**@b@     * The interface Operation.@b@     */@b@    public static interface Operation<T> {@b@@b@        /**@b@         * Operate t.@b@         *@b@         * @param jedis the jedis@b@         * @return the t@b@         */@b@        public T operate(Jedis jedis);@b@    }@b@@b@    /**@b@     * The interface Transactional operation.@b@     */@b@    public static interface TransactionalOperation<T> {@b@@b@        /**@b@         * Operation t.@b@         *@b@         * @param tx the tx@b@         * @return the t@b@         */@b@        public T operation(Transaction tx);@b@    }@b@@b@    public void setPool(JedisPool pool) {@b@        this.pool = pool;@b@    }@b@@b@}

2. 依赖类SerializeUtils、JedisPoolFactoryBean

SerializeUtils.java

import java.io.ByteArrayInputStream;@b@import java.io.ByteArrayOutputStream;@b@import java.io.ObjectInputStream;@b@import java.io.ObjectOutputStream;@b@@b@public class SerializeUtils {@b@    @b@    public static byte[] serializeObject(Object obj) {        @b@        ByteArrayOutputStream byteOutputStream = null;@b@        ObjectOutputStream objectOutputStream = null;@b@        @b@        try{@b@            byteOutputStream = new ByteArrayOutputStream();@b@            objectOutputStream = new ObjectOutputStream(byteOutputStream);@b@            @b@            objectOutputStream.writeObject(obj);@b@            objectOutputStream.flush();@b@            @b@            return byteOutputStream.toByteArray();@b@        }catch(Exception e){@b@            e.printStackTrace();@b@        } finally {@b@            if(null != objectOutputStream){@b@                try{@b@                    objectOutputStream.close();@b@                    byteOutputStream.close();@b@                }catch(Exception e){@b@                    e.printStackTrace();@b@                }@b@            }@b@        }      @b@        @b@        return null;@b@    }@b@    @b@    public static Object deserializeObject(byte[] bytes) {@b@        ByteArrayInputStream byteInputStream = null;@b@        ObjectInputStream objectInputStream = null;@b@        @b@        try{@b@            byteInputStream = new ByteArrayInputStream(bytes);@b@            objectInputStream = new ObjectInputStream(byteInputStream);@b@            @b@            return objectInputStream.readObject();@b@            @b@        }catch(Exception e){@b@            e.printStackTrace();@b@        }finally {@b@            if(null != objectInputStream){@b@                try{@b@                    objectInputStream.close();@b@                    byteInputStream.close();@b@                }catch(Exception e){@b@                    e.printStackTrace();@b@                }@b@            }@b@        }@b@        return null;@b@    }@b@    @b@}

JedisPoolFactoryBean.java

import org.springframework.beans.factory.FactoryBean;@b@import redis.clients.jedis.JedisPool;@b@import redis.clients.jedis.JedisPoolConfig;@b@@b@public class JedisPoolFactoryBean implements FactoryBean<JedisPool> {@b@@b@    private JedisPoolConfig poolConfig;@b@    private String host;@b@    private String password;@b@    private Integer timeout;@b@    private Integer port;@b@    private Integer database;@b@@b@    public void setPoolConfig(JedisPoolConfig poolConfig) {@b@        this.poolConfig = poolConfig;@b@    }@b@@b@    public void setHost(String host) {@b@        this.host = host;@b@    }@b@@b@    public void setPassword(String password) {@b@        this.password = password;@b@    }@b@@b@    public void setTimeout(Integer timeout) {@b@        this.timeout = timeout;@b@    }@b@@b@    public void setPort(Integer port) {@b@        this.port = port;@b@    }@b@@b@    public void setDatabase(Integer database) {@b@        this.database = database;@b@    }@b@@b@    public JedisPool getObject() throws Exception {@b@        JedisPool jp = new JedisPool(poolConfig,host,port,timeout,password,database);@b@        return jp;@b@    }@b@@b@    public Class<?> getObjectType() {@b@        return JedisPool.class;@b@    }@b@@b@    public boolean isSingleton() {@b@        return true;@b@    }@b@@b@}

3. spring-redis.xml配置

<?xml version="1.0" encoding="UTF-8"?>@b@<beans xmlns="http://www.springframework.org/schema/beans"@b@	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"@b@	xsi:schemaLocation="http://www.springframework.org/schema/beans   @b@@b@	<!-- POOL配置 -->@b@	<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">@b@		<!-- <property name="maxActive" value="${redis.pool.maxActive}" /> -->@b@		<!-- <property name="maxWait" value="${redis.pool.maxWait}" /> -->@b@		<property name="maxIdle" value="${redis.pool.maxIdle}" />@b@		<property name="testOnBorrow" value="${redis.pool.testOnBorrow}" />@b@	</bean>@b@@b@	<bean id="jedisPool" class="com.xwood.redis.JedisPoolFactoryBean">@b@		<property name="poolConfig" ref="jedisPoolConfig" />@b@		<property name="host" value="${redis.ip1}" />@b@		<property name="password">@b@			<null />@b@		</property>@b@		<!-- <property name="password" value="${redis.password}" /> -->@b@		<property name="port" value="${redis.port1}" />@b@		<property name="timeout" value="${redis.timeout}" />@b@		<property name="database" value="${redis.database}" />@b@	</bean>@b@@b@@b@	<bean id="redisUtils" class="com.xwood.redis.util.RedisUtils">@b@		<property name="pool" ref="jedisPool" />@b@	</bean>@b@@b@</beans>

4. config.properties配置内容

redis.pool.maxActive=500@b@redis.pool.maxWait=1000@b@redis.pool.maxIdle=10000@b@redis.pool.testOnBorrow=false@b@redis.ip1=127.0.0.1@b@#redis.ip1=127.0.0.1@b@redis.password=@b@redis.port1=6379@b@redis.timeout=100000@b@redis.database=0

5. pom.xml依赖项

<dependency>@b@	 <groupId>redis.clients</groupId>@b@	 <artifactId>jedis</artifactId>@b@	 <version>2.6.0</version>@b@ </dependency>
<<热门下载>>