标签:redis
非切片redis:表示只有一个redis缓存服务器
package com.mifi.common; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPoolConfig; public class RedisClient { private Jedis jedis;//非切片客户端连接 private JedisPool jedisPool;//非切片连接池 public Jedis getJedis() { return jedis; } public void setJedis(Jedis jedis) { this.jedis = jedis; } public JedisPool getJedisPool() { return jedisPool; } public void setJedisPool(JedisPool jedisPool) { this.jedisPool = jedisPool; } public RedisClient() { initialPool(); jedis = jedisPool.getResource(); } public void initialPool(){ // 池基本配置 JedisPoolConfig config = new JedisPoolConfig(); config.setMaxIdle(5); config.setMaxWaitMillis(2000000l); config.setTestOnBorrow(false); // jedisPool = new JedisPool(poolConfig, host, port, timeout) } }
也可使用配置:
<context:property-placeholder location="classpath*:redis.properties" /> <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"> <property name="maxIdle" value="${redis.pool.maxIdle}" /> <property name="maxWaitMillis" value="${redis.pool.setMaxWaitMillis}" /> <property name="testOnBorrow" value="${redis.pool.testOnBorrow}" /> </bean> <bean id="jedisPool" class="redis.clients.jedis.JedisPool"> <constructor-arg index="0" ref="jedisPoolConfig" /> <constructor-arg index="1" value="${redis.hostname}" type="String"/> <constructor-arg index="2" value="${redis.port}" type="int" /> <constructor-arg index="3" value="${redis.timeout}" type="int" /> </bean>
属性文件配置
redis.pool.maxIdle=5 redis.pool.setMaxWaitMillis=1000 redis.pool.testOnBorrow=true redis.hostname=192.168.0.10 redis.port=6379 redis.timeout=600
获取实例:
配置redispool的bean
@Resource(name="jedisPool") JedisPool jedisPool;
在方法中获取redis实例,其实是jedis,目前的配置,存放的value只支持String
Jedis jedis = jedisPool.getResource(); jedis.setex("key", time, "value");//time:缓存保存的时间,单位是秒
从redis中获取
Jedis jedis = jedisPool.getResource(); String jedisPhone = jedis.get("key");
从redis中移除
jedis.del("key");
使用的jar包:
jedis-2.7.3.jar
本文出自 “东方小阁” 博客,请务必保留此出处http://lailai.blog.51cto.com/3362373/1715364
标签:redis
原文地址:http://lailai.blog.51cto.com/3362373/1715364