Redis实战之Redis + Jedis
2016-03-03 15:10
363 查看
用Memcached,对于缓存对象大小有要求,单个对象不得大于1MB,且不支持复杂的数据类型,譬如SET
等。基于这些限制,有必要考虑Redis!
需要Jedis就从Maven获取吧!
Maven Pom.xml
如果只是简单使用Jedis,以下这么几行代码足够:
Jedis jedis = new Jedis("10.11.20.140");
String keys = "name";
// 删数据
jedis.del(keys);
// 存数据
jedis.set(keys, "snowolf");
// 取数据
String value = jedis.get(keys);
System.out.println(value);
在静态代码段中完成初始化:
private static JedisPool pool;
static {
ResourceBundle bundle = ResourceBundle.getBundle("redis");
if (bundle == null) {
throw new IllegalArgumentException(
"[redis.properties] is not found!");
}
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxActive(Integer.valueOf(bundle
.getString("redis.pool.maxActive")));
config.setMaxIdle(Integer.valueOf(bundle
.getString("redis.pool.maxIdle")));
config.setMaxWait(Long.valueOf(bundle.getString("redis.pool.maxWait")));
config.setTestOnBorrow(Boolean.valueOf(bundle
.getString("redis.pool.testOnBorrow")));
config.setTestOnReturn(Boolean.valueOf(bundle
.getString("redis.pool.testOnReturn")));
pool = new JedisPool(config, bundle.getString("redis.ip"),
Integer.valueOf(bundle.getString("redis.port")));
}
然后,修改前面那段jedis操作Redis
// 从池中获取一个Jedis对象
Jedis jedis = pool.getResource();
String keys = "name";
// 删数据
jedis.del(keys);
// 存数据
jedis.set(keys, "snowolf");
// 取数据
String value = jedis.get(keys);
System.out.println(value);
// 释放对象池
pool.returnResource(jedis);
改为从对象池中,获取Jedis实例:
// 从池中获取一个Jedis对象
Jedis jedis = pool.getResource();
切记,最后使用后,释放Jedis对象:
// 释放对象池
pool.returnResource(jedis);
如果有必要,可以用Spring封装初始化:
等。基于这些限制,有必要考虑Redis!
一、简单使用Jedis
需要Jedis就从Maven获取吧!Maven Pom.xml
<dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.1.0</version> <type>jar</type> <scope>compile</scope> </dependency>
如果只是简单使用Jedis,以下这么几行代码足够:
Jedis jedis = new Jedis("10.11.20.140");
String keys = "name";
// 删数据
jedis.del(keys);
// 存数据
jedis.set(keys, "snowolf");
// 取数据
String value = jedis.get(keys);
System.out.println(value);
二、池化使用Jedis
#最大分配的对象数 redis.pool.maxActive=1024 #最大能够保持idel状态的对象数 redis.pool.maxIdle=200 #当池内没有返回对象时,最大等待时间 redis.pool.maxWait=1000 #当调用borrow Object方法时,是否进行有效性检查 redis.pool.testOnBorrow=true #当调用return Object方法时,是否进行有效性检查 redis.pool.testOnReturn=true #IP redis.ip=10.11.20.140 #Port redis.port=6379
在静态代码段中完成初始化:
private static JedisPool pool;
static {
ResourceBundle bundle = ResourceBundle.getBundle("redis");
if (bundle == null) {
throw new IllegalArgumentException(
"[redis.properties] is not found!");
}
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxActive(Integer.valueOf(bundle
.getString("redis.pool.maxActive")));
config.setMaxIdle(Integer.valueOf(bundle
.getString("redis.pool.maxIdle")));
config.setMaxWait(Long.valueOf(bundle.getString("redis.pool.maxWait")));
config.setTestOnBorrow(Boolean.valueOf(bundle
.getString("redis.pool.testOnBorrow")));
config.setTestOnReturn(Boolean.valueOf(bundle
.getString("redis.pool.testOnReturn")));
pool = new JedisPool(config, bundle.getString("redis.ip"),
Integer.valueOf(bundle.getString("redis.port")));
}
然后,修改前面那段jedis操作Redis
// 从池中获取一个Jedis对象
Jedis jedis = pool.getResource();
String keys = "name";
// 删数据
jedis.del(keys);
// 存数据
jedis.set(keys, "snowolf");
// 取数据
String value = jedis.get(keys);
System.out.println(value);
// 释放对象池
pool.returnResource(jedis);
改为从对象池中,获取Jedis实例:
// 从池中获取一个Jedis对象
Jedis jedis = pool.getResource();
切记,最后使用后,释放Jedis对象:
// 释放对象池
pool.returnResource(jedis);
三、Spring封装参考
如果有必要,可以用Spring封装初始化:<context:property-placeholder location="classpath:redis.properties" /> <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig" > <property name="maxActive" value="${redis.pool.maxActive}" /> <property name="maxIdle" value="${redis.pool.maxIdle}" /> <property name="maxWait" value="${redis.pool.maxWait}" /> <property name="testOnBorrow" value="${redis.pool.testOnBorrow}" /> </bean> <bean id="shardedJedisPool" class="redis.clients.jedis.ShardedJedisPool" > <constructor-arg index="0" ref="jedisPoolConfig" /> <constructor-arg index="1"> <list> <bean class="redis.clients.jedis.JedisShardInfo"> <constructor-arg index="0" value="${redis1.ip}" /> <constructor-arg index="1" value="${redis.port}" type="int" /> </bean> <bean class="redis.clients.jedis.JedisShardInfo"> <constructor-arg index="0" value="${redis2.ip}" /> <constructor-arg index="1" value="${redis.port}" type="int" /> </bean> </list> </constructor-arg> </bean>
private ApplicationContext app; private ShardedJedisPool pool; @Before public void before() throws Exception { app = new ClassPathXmlApplicationContext("applicationContext.xml"); pool = (ShardedJedisPool) app.getBean("shardedJedisPool"); } @Test public void test() { // 从池中获取一个Jedis对象 ShardedJedis jedis = pool.getResource(); String keys = "name"; String value = "snowolf"; // 删数据 jedis.del(keys); // 存数据 jedis.set(keys, value); // 取数据 String v = jedis.get(keys); System.out.println(v); // 释放对象池 pool.returnResource(jedis); assertEquals(value, v); }当然,Spring提供了对于Redis的专门支持:spring-data-redis。
相关文章推荐
- windows下安装redis
- C++自制Redis 数据库 (十五)【重构】数据库学习(三)
- springmvc redis 简单实例
- redis的安装与一些基础操作
- Redis数据类型及操作详解
- redis 在spring boot工程中的应用(一)
- Redis 常用操作命令
- Redis安装、配置及拓展使用总结
- 国内外三个不同领域巨头分享的Redis实战经验及使用场景
- Redis的5种数据类型
- Python+Scrapy+Redis+Mongodb 环境安装
- SpringMVC + MyBatis + Mysql + Redis(作为二级缓存) 配置
- redis 3.0.501 安装:
- java对redis的基本操作
- redis的数据持久化
- redis-windows服务安装
- linux redis安装
- redis
- linux 安装redis 部署 启动 停止
- php对redis的zset(有序集合)操作