一、redis配置 1、啟動redis 這裡使用的是windows版本的redis,直接解壓使用就可以了。 註意1: 當運行redis-server時需要註意的是不同的電腦因為配置問題可能無法雙擊啟動。所以需要通過命令提示符的方式進行啟動 這個界面表示redis已經啟動,預設配置 redis.hos ...
一、redis配置
1、啟動redis
這裡使用的是windows版本的redis,直接解壓使用就可以了。
註意1: 當運行redis-server時需要註意的是不同的電腦因為配置問題可能無法雙擊啟動。所以需要通過命令提示符的方式進行啟動
這個界面表示redis已經啟動,預設配置 redis.host=localhost redis.port=6379
2、applicationContext-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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" 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"> <context:component-scan base-package="com.zhongwu.redis.dao"/> <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"> <property name="maxActive" value="50" /> <property name="maxIdle" value="8" /> <property name="maxWait" value="1000" /> <property name="testOnBorrow" value="true"/> <property name="testOnReturn" value="true"/> <!-- <property name="testWhileIdle" value="true"/> --> </bean> <bean id="shardedJedisPool" class="redis.clients.jedis.ShardedJedisPool" scope="singleton"> <constructor-arg index="0" ref="jedisPoolConfig" /> <constructor-arg index="1"> <list> <bean class="redis.clients.jedis.JedisShardInfo"> <constructor-arg name="host" value="localhost" /> <constructor-arg name="port" value="6379" /> <constructor-arg name="timeout" value="0" /> <constructor-arg name="weight" value="1" /> </bean> </list> </constructor-arg> </bean> </beans>
引入jar包,這裡需要註意的是redis版本問題,版本更新中有些參數名稱更改,此處根據applicationContext-redis.xml配置中參數選擇2.1.0版本進行配置。
<!-- redis --> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.1.0</version> </dependency>
3、redis使用相關介面方法
RedisDataSource.java
package com.zhongwu.redis.dao; import redis.clients.jedis.ShardedJedis; public interface RedisDataSource { public abstract ShardedJedis getRedisClient(); public void returnResource(ShardedJedis shardedJedis); public void returnResource(ShardedJedis shardedJedis,boolean broken); }
RedisClientTemplate.java
package com.zhongwu.redis.dao.impl; import java.util.Collection; import java.util.List; import java.util.Map; import java.util.Set; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.stereotype.Repository; import com.zhongwu.redis.dao.RedisDataSource; import redis.clients.jedis.BinaryClient.LIST_POSITION; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisShardInfo; import redis.clients.jedis.ShardedJedis; import redis.clients.jedis.ShardedJedisPipeline; import redis.clients.jedis.SortingParams; import redis.clients.jedis.Tuple; @Repository("redisClientTemplate") public class RedisClientTemplate { private static final Logger log = LoggerFactory.getLogger(RedisClientTemplate.class); @Autowired private RedisDataSource redisDataSource; public void disconnect() { ShardedJedis shardedJedis = redisDataSource.getRedisClient(); shardedJedis.disconnect(); } /** * 通過配置獲取redis連接 * * return * */ public static RedisClientTemplate getRedis(){ ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:/META-INF/spring/applicationContext2.xml"); RedisClientTemplate redisClient = (RedisClientTemplate)ac.getBean("redisClientTemplate"); return redisClient; } /** * 設置單個值 * * @param key * @param value * @return */ public String set(String key, String value) { String result = null; ShardedJedis shardedJedis = redisDataSource.getRedisClient(); if (shardedJedis == null) { return result; } boolean broken = false; try { result = shardedJedis.set(key, value); } catch (Exception e) { log.error(e.getMessage(), e); broken = true; } finally { redisDataSource.returnResource(shardedJedis, broken); } return result; } /** * 獲取單個值 * * @param key * @return */ public String get(String key) { String result = null; ShardedJedis shardedJedis = redisDataSource.getRedisClient(); if (shardedJedis == null) { return result; } boolean broken = false; try { result = shardedJedis.get(key); } catch (Exception e) { log.error(e.getMessage(), e); broken = true; } finally { redisDataSource.returnResource(shardedJedis, broken); } return result; } public Boolean exists(String key) { Boolean result = false; ShardedJedis shardedJedis = redisDataSource.getRedisClient(); if (shardedJedis == null) { return result; } boolean broken = false; try { result = shardedJedis.exists(key); } catch (Exception e) { log.error(e.getMessage(), e); broken = true; } finally { redisDataSource.returnResource(shardedJedis, broken); } return result; } public String type(String key) { String result = null; ShardedJedis shardedJedis = redisDataSource.getRedisClient(); if (shardedJedis == null) { return result; } boolean broken = false; try { result = shardedJedis.type(key); } catch (Exception e) { log.error(e.getMessage(), e); broken = true; } finally { redisDataSource.returnResource(shardedJedis, broken); } return result; } /** * 在某段時間後實現 * * @param key * @param unixTime * @return */ public Long expire(String key, int seconds) { Long result = null; ShardedJedis shardedJedis = redisDataSource.getRedisClient(); if (shardedJedis == null) { return result; } boolean broken = false; try { result = shardedJedis.expire(key, seconds); } catch (Exception e) { log.error(e.getMessage(), e); broken = true; } finally { redisDataSource.returnResource(shardedJedis, broken); } return result; } /** * 在某個時間點失效 * * @param key * @param unixTime * @return */ public Long expireAt(String key, long unixTime) { Long result = null; ShardedJedis shardedJedis = redisDataSource.getRedisClient(); if (shardedJedis == null) { return result; } boolean broken = false; try { result = shardedJedis.expireAt(key, unixTime); } catch (Exception e) { log.error(e.getMessage(), e); broken = true; } finally { redisDataSource.returnResource(shardedJedis, broken); } return result; } public Long ttl(String key) { Long result = null; ShardedJedis shardedJedis = redisDataSource.getRedisClient(); if (shardedJedis == null) { return result; } boolean broken = false; try { result = shardedJedis.ttl(key); } catch (Exception e) { log.error(e.getMessage(), e); broken = true; } finally { redisDataSource.returnResource(shardedJedis, broken); } return result; } public boolean setbit(String key, long offset, boolean value) { ShardedJedis shardedJedis = redisDataSource.getRedisClient(); boolean result = false; if (shardedJedis == null) { return result; } boolean broken = false; try { result = shardedJedis.setbit(key, offset, value); } catch (Exception e) { log.error(e.getMessage(), e); broken = true; } finally { redisDataSource.returnResource(shardedJedis, broken); } return result; } public boolean getbit(String key, long offset) { ShardedJedis shardedJedis = redisDataSource.getRedisClient(); boolean result = false; if (shardedJedis == null) { return result; } boolean broken = false; try { result = shardedJedis.getbit(key, offset); } catch (Exception e) { log.error(e.getMessage(), e); broken = true; } finally { redisDataSource.returnResource(shardedJedis, broken); } return result; } public long setrange(String key, long offset, String value) { ShardedJedis shardedJedis = redisDataSource.getRedisClient(); long result = 0; if (shardedJedis == null) { return result; } boolean broken = false; try { result = shardedJedis.setrange(key, offset, value); } catch (Exception e) { log.error(e.getMessage(), e); broken = true; } finally { redisDataSource.returnResource(shardedJedis, broken); } return result; } public String getrange(String key, long startOffset, long endOffset) { ShardedJedis shardedJedis = redisDataSource.getRedisClient(); String result = null; if (shardedJedis == null) { return result; } boolean broken = false; try { result = shardedJedis.getrange(key, startOffset, endOffset); } catch (Exception e) { log.error(e.getMessage(), e); broken = true; } finally { redisDataSource.returnResource(shardedJedis, broken); } return result; } public String getSet(String key, String value) { String result = null; ShardedJedis shardedJedis = redisDataSource.getRedisClient(); if (shardedJedis == null) { return result; } boolean broken = false; try { result = shardedJedis.getSet(key, value); } catch (Exception e) { log.error(e.getMessage(), e); broken = true; } finally { redisDataSource.returnResource(shardedJedis, broken); } return result; } public Long setnx(String key, String value) { Long result = null; ShardedJedis shardedJedis = redisDataSource.getRedisClient(); if (shardedJedis == null) { return result; } boolean broken = false; try { result = shardedJedis.setnx(key, value); } catch (Exception e) { log.error(e.getMessage(), e); broken = true; } finally { redisDataSource.returnResource(shardedJedis, broken); } return result; } public String setex(String key, int seconds, String value) { String result = null; ShardedJedis shardedJedis = redisDataSource.getRedisClient(); if (shardedJedis == null) { return result; } boolean broken = false; try { result = shardedJedis.setex(key, seconds, value); } catch (Exception e) { log.error(e.getMessage(), e); broken = true; } finally { redisDataSource.returnResource(shardedJedis, broken); } return result; } public Long decrBy(String key, long integer) { Long result = null; ShardedJedis shardedJedis = redisDataSource.getRedisClient(); if (shardedJedis == null) { return result; } boolean broken = false; try { result = shardedJedis.decrBy(key, integer); } catch (Exception e) { log.error(e.getMessage(), e); broken = true; } finally { redisDataSource.returnResource(shardedJedis, broken); } return result; } public Long decr(String key) { Long result = null; ShardedJedis shardedJedis = redisDataSource.getRedisClient(); if (shardedJedis == null) { return result; } boolean broken = false; try { result = shardedJedis.decr(key); } catch (Exception e) { log.error(e.getMessage(), e); broken = true; } finally { redisDataSource.returnResource(shardedJedis, broken); } return result; } public Long incrBy(String key, long integer) { Long result = null; ShardedJedis shardedJedis = redisDataSource.getRedisClient(); if (shardedJedis == null) { return result; } boolean broken = false; try { result = shardedJedis.incrBy(key, integer); } catch (Exception e) { log.error(e.getMessage(), e); broken = true; } finally { redisDataSource.returnResource(shardedJedis, broken); } return result; } public Long incr(String key) { Long result = null; ShardedJedis shardedJedis = redisDataSource.getRedisClient(); if (shardedJedis == null) { return result; } boolean broken = false; try { result = shardedJedis.incr(key); } catch (Exception e) { log.error(e.getMessage(), e); broken = true; } finally { redisDataSource.returnResource(shardedJedis, broken); } return result; } public Long append(String key, String value) { Long result = null; ShardedJedis shardedJedis = redisDataSource.getRedisClient(); if (shardedJedis == null) { return result; } boolean broken = false; try { result = shardedJedis.append(key, value); } catch (Exception e) { log.error(e.getMessage(), e); broken = true; } finally { redisDataSource.returnResource(shardedJedis, broken); } return result; } public String substr(String key, int start, int end) { String result = null; ShardedJedis shardedJedis = redisDataSource.getRedisClient(); if (shardedJedis == null) { return result; } boolean broken = false; try { result = shardedJedis.substr(key, start, end); } catch (Exception e) { log.error(e.getMessage(), e); broken = true; } finally { redisDataSource.returnResource(shardedJedis, broken); } return result; } public Long hset(String key, String field, String value) { Long result = null; ShardedJedis shardedJedis = redisDataSource.getRedisClient(); if (shardedJedis == null) { return result; } boolean broken = false; try { result = shardedJedis.hset(key, field, value); } catch (Exception e) { log.error(e.getMessage(), e); broken = true; } finally { redisDataSource.returnResource(shardedJedis, broken); } return result; } public String hget(String key, String field) { String result = null; ShardedJedis shardedJedis = redisDataSource.getRedisClient(); if (shardedJedis == null) { return result; } boolean broken = false; try { result = shardedJedis.hget(key, field); } catch (Exception e) { log.error(e.getMessage(), e); broken = true; } finally { redisDataSource.returnResource(shardedJedis, broken); } return result; } public Long hsetnx(String key, String field, String value) { Long result = null; ShardedJedis shardedJedis = redisDataSource.getRedisClient(); if (shardedJedis == null) { return result; } boolean broken = false; try { result = shardedJedis.hsetnx(key, field, value); } catch (Exception e) { log.error(e.getMessage(), e); broken = true; } finally { redisDataSource.returnResource(shardedJedis, broken); } return result; } public String hmset(String key, Map<String, String> hash) { String result = null; ShardedJedis shardedJedis = redisDataSource.getRedisClient(); if (shardedJedis == null) { return result; } boolean broken = false; try { result = shardedJedis.hmset(key, hash); } catch (Exception e) { log.error(e.getMessage(), e); broken = true; } finally { redisDataSource.returnResource(shardedJedis, broken); } return result; } public List<String> hmget(String key, String... fields) { List<String> result = null; ShardedJedis shardedJedis = redisDataSource.getRedisClient(); if (shardedJedis == null) { return result; } boolean broken = false; try { result = shardedJedis.hmget(key, fields); } catch (Exception e) { log.error(e.getMessage(), e); broken = true; } finally { redisDataSource.returnResource(shardedJedis, broken); } return result; } public Long hincrBy(String key, String field, long value) { Long result = null; ShardedJedis shardedJedis = redisDataSource.getRedisClient(); if (shardedJedis == null) { return result; } boolean broken = false; try { result = shardedJedis.hincrBy(key, field, value); } catch (Exception e) { log.error(e.getMessage(), e); broken = true; } finally { redisDataSource.returnResource(shardedJedis, broken); } return result; } public Boolean hexists(String key, String field) { Boolean result = false; ShardedJedis shardedJedis = redisDataSource.getRedisClient(); if (shardedJedis == null) { return result; } boolean broken = false; try { result = shardedJedis.hexists(key, field); } catch (Exception e) { log.error(e.getMessage(), e); broken = true; } finally { redisDataSource.returnResource(shardedJedis, broken); } return result; } public Long del(String key) { Long result = null; ShardedJedis shardedJedis = redisDataSource.getRedisClient(); if (shardedJedis == null) { return result; } boolean broken = false; try { result = shardedJedis.del(key); } catch (Exception e) { log.error(e.getMessage(), e); broken = true; } finally { redisDataSource.returnResource(shardedJedis, broken); } return result; } public Long hdel(String key, String field) { Long result = null; ShardedJedis shardedJedis = redisDataSource.getRedisClient(); if (shardedJedis == null) { return result; } boolean broken = false; try { result = shardedJedis.hdel(key, field); } catch (Exception e) { log.error(e.getMessage(), e); broken = true; } finally { redisDataSource.returnResource(shardedJedis, broken); } return result; } public Long hlen(String key) { Long result = null; ShardedJedis shardedJedis = redisDataSource.getRedisClient(); if (shardedJedis == null) { return result; } boolean broken = false; try { result = shardedJedis.hlen(key); } catch (Exception e) { log.error(e.getMessage(), e); broken = true; } finally { redisDataSource.returnResource(shardedJedis, broken); } return result; } public Set<String> hkeys(String key) { Set<String> result = null; ShardedJedis shardedJedis = redisDataSource.getRedisClient(); if (shardedJedis == null) { return result; } boolean broken = false; try { result = shardedJedis.hkeys(key); } catch (Exception e) { log.error(e.getMessage(), e); broken = true; } finally { redisDataSource.returnResource(shardedJedis, broken); } return result; } public List<String> hvals(String key) { List<String> result = null; ShardedJedis shardedJedis = redisDataSource.getRedisClient(); if (shardedJedis == null) { return result; } boolean broken = false; try { result = shardedJedis.hvals(key); } catch (Exception e) { log.error(e.getMessage(), e); broken = true; } finally { redisDataSource.returnResource(shardedJedis, broken); } return result; } public Map<String, String> hgetAll(String key) { Map<String, String> result = null; ShardedJedis shardedJedis = redisDataSource.getRedisClient(); if (shardedJedis == null) { return