码迷,mamicode.com
首页 > 编程语言 > 详细

java操作redis

时间:2015-07-30 16:58:22      阅读:154      评论:0      收藏:0      [点我收藏+]

标签:redis   实例   jedis   

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", "testValue0");
    }

    public String testGet(String keyName) {
        return jedis.get(keyName);
    }
    public static void main(String[] args) {

        //new Main().testInsert();
        System.out.println(new Main().testGet("testKey0"));
    }
}

技术分享

技术分享

版权声明:本文为博主原创文章,未经博主允许不得转载。

java操作redis

标签:redis   实例   jedis   

原文地址:http://blog.csdn.net/weiyongxuan/article/details/47151783

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!