一,简单Jedis读写
Jedis jedis=new Jedis("127.0.0.1",6379);//连接的是我本地的服务 System.out.println("成功连接到服务器=="); /*存取string类型*/ jedis.set("name", "lhc"); model.addAttribute("name",jedis.get("name")); /*存取hash类型*/ jedis.hset("hash", "k1", "v1"); jedis.hset("hash", "k2", "v2"); jedis.hset("hash", "k3", "v3"); model.addAttribute("hash",jedis.hvals("hash")); /*存取list*/ jedis.lpush("student", "lhc"); jedis.lpush("student", "水田如雅"); jedis.lpush("student", "杰伦哥哥"); List<String> stuList=jedis.lrange("student", 0, 2); model.addAttribute("stuList",stuList.toString()); /*存取set*/ jedis.sadd("person","lhc"); jedis.sadd("person","水田"); jedis.sadd("person","小白"); model.addAttribute("people",jedis.smembers("person")); /*存取zset*/ jedis.zadd("animal", 0, "cat"); jedis.zadd("animal", 0, "dog"); jedis.zadd("animal", 0, "sheep"); model.addAttribute("animal",jedis.zrange("animal", 0, 2)); //查看服务是否允许 System.out.println("服务器正在连通中。。。状态:"+jedis.ping());
二,使用连接池读写
JedisPool与ShardedJedisPool:
JedisPool连一台Redis,ShardedJedisPool连Redis集群,通过一致性哈希算法决定把数据存到哪台上,算是一种客户端负载均衡,所以添加是用这个(Redis 3.0之后支持服务端负载均衡) 。
使用SharedJedisPool操作Redis集群工具类:
public class RedisDao { private static final Logger logger = Logger.getLogger(RedisDao.class); @Autowired private ShardedJedisPool shardedJedisPool; public void set(String key, String value) { ShardedJedis jedis = shardedJedisPool.getResource(); try { jedis.set(key, value); } catch (Exception e) { logger.error(e.getMessage(), e); shardedJedisPool.returnBrokenResource(jedis); } finally { shardedJedisPool.returnResource(jedis); } return; } public String get(String key) { ShardedJedis jedis = shardedJedisPool.getResource(); String result = ""; try { result = jedis.get(key); } catch (Exception e) { logger.error(e.getMessage(), e); shardedJedisPool.returnBrokenResource(jedis); } finally { shardedJedisPool.returnResource(jedis); } return result; } public Long hset(String key, String field, String value) { ShardedJedis jedis = shardedJedisPool.getResource(); Long result = null; try { result = jedis.hset(key, field, value); } catch (Exception e) { logger.error(e.getMessage(), e); shardedJedisPool.returnBrokenResource(jedis); } finally { shardedJedisPool.returnResource(jedis); } return result; } public String hget(String key, String field) { ShardedJedis jedis = shardedJedisPool.getResource(); String result = null; try { result = jedis.hget(key, field); } catch (Exception e) { logger.error(e.getMessage(), e); shardedJedisPool.returnBrokenResource(jedis); } finally { shardedJedisPool.returnResource(jedis); } return result; } public List<String> lrange(String key,long start,long end) { ShardedJedis jedis = shardedJedisPool.getResource(); List<String> result = null; try { result = jedis.lrange(key, start, end);; } catch (Exception e) { logger.error(e.getMessage(), e); shardedJedisPool.returnBrokenResource(jedis); } finally { shardedJedisPool.returnResource(jedis); } return result; } public Long del(String key) { ShardedJedis jedis = shardedJedisPool.getResource(); Long result = null; try { result = jedis.del(key); } catch (Exception e) { logger.error(e.getMessage(), e); shardedJedisPool.returnBrokenResource(jedis); } finally { shardedJedisPool.returnResource(jedis); } return result; } public void rpush(String key, String... value) { ShardedJedis jedis = shardedJedisPool.getResource(); try { jedis.rpush(key,value); } catch (Exception e) { logger.error(e.getMessage(), e); shardedJedisPool.returnBrokenResource(jedis); } finally { shardedJedisPool.returnResource(jedis); } return; } public void lpush(String key, String... value) { ShardedJedis jedis = shardedJedisPool.getResource(); try { jedis.lpush(key,value); } catch (Exception e) { logger.error(e.getMessage(), e); shardedJedisPool.returnBrokenResource(jedis); } finally { shardedJedisPool.returnResource(jedis); } return; } public void lpop(String key) { ShardedJedis jedis = shardedJedisPool.getResource(); try { jedis.lpop(key); } catch (Exception e) { logger.error(e.getMessage(), e); shardedJedisPool.returnBrokenResource(jedis); } finally { shardedJedisPool.returnResource(jedis); } return; } public void rpop(String key) { ShardedJedis jedis = shardedJedisPool.getResource(); try { jedis.rpop(key); } catch (Exception e) { logger.error(e.getMessage(), e); shardedJedisPool.returnBrokenResource(jedis); } finally { shardedJedisPool.returnResource(jedis); } return; } public void lrem(String key,int count,String value) { ShardedJedis jedis = shardedJedisPool.getResource(); try { jedis.lrem(key,count,value); } catch (Exception e) { logger.error(e.getMessage(), e); shardedJedisPool.returnBrokenResource(jedis); } finally { shardedJedisPool.returnResource(jedis); } return; } /** * 添加key value 并且设置存活时间 * @param key * @param value * @param liveTime */ public void set(String key,String value,int liveTime){ ShardedJedis jedis = shardedJedisPool.getResource(); try { this.set(key, value); jedis.expire(key, liveTime); } catch (Exception e) { logger.error(e.getMessage(), e); shardedJedisPool.returnBrokenResource(jedis); } finally { shardedJedisPool.returnResource(jedis); } } /** * 检查key是否已经存在 * @param key * @return */ public boolean exists(String key){ ShardedJedis jedis = shardedJedisPool.getResource(); boolean result =false; try { result=jedis.exists(key); } catch (Exception e) { logger.error(e.getMessage(), e); shardedJedisPool.returnBrokenResource(jedis); } finally { shardedJedisPool.returnResource(jedis); } return result; } /** * 获取一个key的模糊匹配总数 * @param key * @return */ public int getKeyCount(String key) { ShardedJedis jedis = shardedJedisPool.getResource(); Set<String> result = null; try { result = jedis.getShard(key).keys(key + "*"); } catch (Exception e) { logger.error(e.getMessage(), e); shardedJedisPool.returnBrokenResource(jedis); } finally { shardedJedisPool.returnResource(jedis); } return result.size(); } /** * 查看redis里有多少数据 */ @SuppressWarnings("deprecation") public long dbSize(){ ShardedJedis jedis = shardedJedisPool.getResource(); Set<String> result = null; try { result=jedis.getShard("").keys("*"); } catch (Exception e) { logger.error(e.getMessage(), e); shardedJedisPool.returnBrokenResource(jedis); } finally { shardedJedisPool.returnResource(jedis); } return result.size(); } public long sadd(String key,String... value){ ShardedJedis jedis = shardedJedisPool.getResource(); long result = 0l; try { result= jedis.sadd(key,value); } catch (Exception e) { logger.error(e.getMessage(), e); shardedJedisPool.returnBrokenResource(jedis); } finally { shardedJedisPool.returnResource(jedis); } return result; } public Boolean sismember(String key,String value){ ShardedJedis jedis = shardedJedisPool.getResource(); Boolean result = false; try { result= jedis.sismember(key,value); } catch (Exception e) { logger.error(e.getMessage(), e); shardedJedisPool.returnBrokenResource(jedis); } finally { shardedJedisPool.returnResource(jedis); } return result; } public Set smembers(String key){ ShardedJedis jedis = shardedJedisPool.getResource(); Set result = null; try { result= jedis.smembers(key); } catch (Exception e) { logger.error(e.getMessage(), e); shardedJedisPool.returnBrokenResource(jedis); } finally { shardedJedisPool.returnResource(jedis); } return result; } public long zadd(String key,int sequence,String value){ ShardedJedis jedis = shardedJedisPool.getResource(); long result = 0l; try { result= jedis.zadd(key,sequence,value); } catch (Exception e) { logger.error(e.getMessage(), e); shardedJedisPool.returnBrokenResource(jedis); } finally { shardedJedisPool.returnResource(jedis); } return result; } public Set<String> zrange(String key,long start,long end){ ShardedJedis jedis = shardedJedisPool.getResource(); Set<String> result = null; try { result= jedis.zrange(key,start,end); } catch (Exception e) { logger.error(e.getMessage(), e); shardedJedisPool.returnBrokenResource(jedis); } finally { shardedJedisPool.returnResource(jedis); } return result; } public String hmset(String key,Map map){ ShardedJedis jedis = shardedJedisPool.getResource(); String result = null; try { result= jedis.hmset(key,map); } catch (Exception e) { logger.error(e.getMessage(), e); shardedJedisPool.returnBrokenResource(jedis); } finally { shardedJedisPool.returnResource(jedis); } return result; } public Map<String,String> hgetAll(String key){ ShardedJedis jedis = shardedJedisPool.getResource(); Map<String,String> result = null; try { result= jedis.hgetAll(key); } catch (Exception e) { logger.error(e.getMessage(), e); shardedJedisPool.returnBrokenResource(jedis); } finally { shardedJedisPool.returnResource(jedis); } return result; } public ShardedJedisPool getShardedJedisPool() { return shardedJedisPool; } public void setShardedJedisPool(ShardedJedisPool shardedJedisPool) { this.shardedJedisPool = shardedJedisPool; } }
项目中还需加入redis的property文件和bean配置:
<!-- 读入属性文件 --> <bean id="propretyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:conf/redis.properties</value> </list> </property> </bean> <!-- redis 配置 --> <bean id="redisDao" class="com.upcloud.demo.redis.dao.RedisDao" scope="prototype"> <property name="shardedJedisPool"> <ref bean="shardedJedisPool"/> </property> </bean> <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"> <property name="maxTotal" value="${redis.maxActive}" /> <!--最大连接数--> <property name="maxIdle" value="${redis.maxIdle}" /> <!--最大空闲数--> <property name="maxWaitMillis" value="${redis.maxWait}" /> <!--最大等待时间ms--> <property name="testOnBorrow" value="false" /> </bean> <bean id="jedis.shardInfo" class="redis.clients.jedis.JedisShardInfo"> <constructor-arg index="0" value="${redis.host}" /> <constructor-arg index="1" value="${redis.port}" /> </bean> <bean id="shardedJedisPool" class="redis.clients.jedis.ShardedJedisPool"> <constructor-arg index="0" ref="jedisPoolConfig" /> <constructor-arg index="1"> <list> <ref bean="jedis.shardInfo" /> </list> </constructor-arg> </bean>
时间: 2024-09-30 09:03:08