设为首页 加入收藏

TOP

java操作redis(一)
2015-11-21 02:05:03 来源: 作者: 【 】 浏览:1
Tags:java 操作 redis

Java操作redis


简单的Jedis实例

package com.weixuan.utils;

import redis.clients.jedis.Jedis;

/**
 * Create by fengtang
 * 2015/7/30
 * JavaRedis
 */
public final class JedisUtils {

    /**
     * 创建一个jedis链接.
     *
     * @return 返回当前创建的redis链接对象
     */
    public static Jedis getJedisConnection() {
        Jedis jedis = new Jedis("localhost", 6379);
        return jedis;
    }
}

//测试
package com.weixuan.test;
import com.weixuan.utils.JedisPoolUtils;
import com.weixuan.utils.JedisUtils;
import org.junit.Test;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

/**
 * Create by fengtang
 * 2015/7/30
 * JavaRedis
 */
public class JedisTest {

    @Test
    public void testJedisUtils() {
        System.out.println("testJedisUtils " + JedisUtils.getJedisConnection());
    }
}

简单的JedisPool应用

**注意,版本Jedis版本是2.1 ,common pool 的版本是1.5
高版本中有些参数的设置不一样,比如setMaxActive**

package com.weixuan.utils; import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPoolConfig; import java.util.ResourceBundle; /** * Create by fengtang * 2015/7/30 * JavaRedis */ public class JedisPoolUtils { public static JedisPool pool; static { ResourceBundle bundle = ResourceBundle.getBundle("jedis"); JedisPoolConfig config = new JedisPoolConfig(); if (bundle == null) { throw new IllegalArgumentException("[jedis.properties] is not found!"); } config.setMaxActive(Integer.valueOf(bundle.getString("redis.pool.maxActive"))); config.setMaxIdle(Integer.valueOf(bundle.getString("redis.pool.maxIdle"))); 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"))); } public static JedisPool getPool() { return pool; } } //测试 package com.weixuan.test; import com.weixuan.utils.JedisPoolUtils; import com.weixuan.utils.JedisUtils; import org.junit.Test; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; /** * Create by fengtang * 2015/7/30 * JavaRedis */ public class JedisTest { @Test public void testJedisPoolUtils() throws Exception { JedisPool pool = JedisPoolUtils.getPool(); System.out.println("testJedisPoolUtils ---pool ------------" + pool); Jedis jedis = pool.getResource(); System.out.println("testJedisPoolUtils --- jedis ------------" + jedis); } } 

高版本中测试

* Jedis 版本2.7 common pool 版本2.4*

 将 config.setMaxActive(Integer.valueOf(bundle.getString("redis.pool.maxActive"))); 换成 config.setMaxTotal(Integer.valueOf(bundle.getString("redis.pool.maxTotal"))); 配置文件也需要改

简单的使用

 package com.weixuan.redis; import com.weixuan.utils.JedisPoolUtils; import redis.clients.jedis.Jedis; /** * Create by fengtang * 2015/7/30 * JavaRedis3 */ public class Main { private Jedis jedis = JedisPoolUtils.getPool().getResource(); public void testInsert() { jedis.set("testKey1", "testValue1"); jedis.set("testKey2", "testValue2"); jedis.set("testKey3", "testValue3"); jedis.set("testKey4", "testValue4"); jedis.set("testKey5", "testValue5"); jedis.set("testKey6", "testValue6"); jedis.set("testKey7", "testValue7"); jedis.set("testKey8", "testValue8"); jedis.set("testKey9", "testValue9"); jedis.set("testKey0", "test
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇kaggle数据挖掘――以Titanic为例.. 下一篇mongoose实现DBRef查找所有子类信..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: