Redis实战之Redis + Jedis

Memcached,对于缓存对象大小有要求,单个对象不得大于1MB,且不支持复杂的数据类型,譬如SET

等。基于这些限制,有必要考虑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。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值