redis学习日志【二、redis+jedis】

现今redis是大体上有3种基于java语言的客户端开发包:

  • Jredis
  • Jedis
  • Redis4J

其中Jedis是官方唯一提供的java语言客户端开发包,且应该是应用最为广泛的。

那便开始实战吧。

一、包的准备。

我使用的是jedis-2.8.0.jar包 下载地址:点击这里

二、实战

1.Hello word仅需短短几段代码:

public static void main(String[] args) {
        //这里放入redis所在的IP,使用默认端口6379
        Jedis jedis=new Jedis("127.0.0.1");     
        String key="name";  
        //删数据
        jedis.del(key);
        //存数据
        jedis.set(key,"wln");
        //取数据
        String value=jedis.get(key);
        System.out.println(value);
    }

可以看出jedis的操作十分的轻便。也和在控制台操作redis的方法想近似。

2.池化使用jedis:
使用commons-pool完成池化实现。
首先创建一个redis.properties配置文件文件

#最大分配对象数
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=127.0.0.1
#port
redis.port=6379

通过单例模型确保配置文件只被读取一次

public class MyProperties extends Properties{
    private static MyProperties mp;

    private MyProperties(){
        try {
            this.load(MyProperties.class.getClassLoader().getResourceAsStream("redis.properties"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static MyProperties getInstance() {
        if(mp==null){
            mp=new MyProperties();
        }
        return mp;
    }
}

在静态块中完成初始化

private static JedisPool pool;  
    static {  
        JedisPoolConfig config = new JedisPoolConfig();  
        // 最大分配对象数
       config.setMaxTotal(Integer.valueOf(MyProperties.getInstance()  
                .getProperty("redis.pool.maxActive")));  
        //最大能保持idel状态的对象数
        config.setMaxIdle(Integer.valueOf(MyProperties.getInstance()    
               .getProperty("redis.pool.maxIdle")));  
        //无返回对象时最大等待时间
        config.setMaxWaitMillis(Long.valueOf(MyProperties.getInstance()
           .getProperty("redis.pool.maxWait")));  
        //调用borrow Object方法时是否检测
        config.setTestOnBorrow(Boolean.valueOf(MyProperties.getInstance()  
            .getProperty("redis.pool.testOnBorrow")));  
        //调用return Object方法时是否检测
        config.setTestOnReturn(Boolean.valueOf(MyProperties.getInstance()  
            .getProperty("redis.pool.testOnReturn")));  
        pool = new JedisPool(config, MyProperties.getInstance().getProperty("redis.ip"),Integer.valueOf(MyProperties.getInstance().getProperty("redis.port")));  
    }  

修改上面的代码

public static void main(String[] args) {
        //改为从池中获取
        Jedis jedis=pool.getResource();
        String key="name";
        //删数据
        jedis.del(key);
        //存数据
        jedis.set(key,"wrm");
        //取数据
        String value=jedis.get(key);
        System.out.println(value);
        //一定要释放对象
        pool.returnResource(jedis);
    }

3.集群
redis是基于Master-Slave的,所以要将redis做成集群的话无非就是多做几套Master-Slave,通过客户端工具,完成一致性哈希

在redis.properties中添加一个新的ip

redis.ip1=192.168.15.212

在前面的static块中实例化两个JedisShardInfo放入list中

edisShardInfo jedis1 = new JedisShardInfo( MyProperties.getInstance().getProperty("redis.ip"),  
                Integer.valueOf(MyProperties.getInstance().getProperty("redis.port")));                       
        JedisShardInfo jedis2 = new JedisShardInfo(MyProperties.getInstance().getProperty("redis.ip"),  
                Integer.valueOf(MyProperties.getInstance().getProperty("redis.port"))); 
        List<JedisShardInfo> list = new LinkedList<JedisShardInfo>();  
        list.add(jedis1);  
        list.add(jedis2);  

初始化ShardedJedisPool代替JedisPool:

ShardedJedisPool pool = new ShardedJedisPool(config, list);  

改由ShardedJedis,获取Jedis对象:

ShardedJedis jedis=pool.getResource();

通过以上的实践,基本的jedis操作已经能够使用。接下来就是有关框架的整合。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值