欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

详谈Jedis连接池的使用

程序员文章站 2024-02-24 18:04:19
1、构建redis连接池,返还到连接池 private static jedispool jedispool = null; private static je...

1、构建redis连接池,返还到连接池

private static jedispool jedispool = null;
private static jedis jedis;

static {
  jedis = getjedispool().getresource();
}

/**
 * 构建redis连接池
 */
public static jedispool getjedispool() {
  if (jedispool == null) {
    jedispoolconfig config = new jedispoolconfig();
    config.setmaxtotal(1024); // 可用连接实例的最大数目,如果赋值为-1,表示不限制.
    config.setmaxidle(5); // 控制一个pool最多有多少个状态为idle(空闲的)jedis实例,默认值也是8
    config.setmaxwaitmillis(1000 * 100); // 等待可用连接的最大时间,单位毫秒,默认值为-1,表示永不超时/如果超过等待时间,则直接抛出异常
    config.settestonborrow(true); // 在borrow一个jedis实例时,是否提前进行validate操作,如果为true,则得到的jedis实例均是可用的
    jedispool = new jedispool(config, "127.0.0.1", 6379);
  }
  return jedispool;
}

/**
 * 释放jedis资源
 */
public static void returnresource(jedis jedis) {
  if (jedis != null) {
    jedis.close();
  }
}

2、 jedis使用

典型的jedis使用方法

public static string get(string key) {
  string value = null;
  jedis jedis = null;
  try {
    jedispool pool = getjedispool();
    jedis = pool.getresource();
    value = jedis.get(key);
  }
  catch (exception e) {
    returnresource(jedis);
    e.printstacktrace();
  }
  finally {
    returnresource(jedis);
  }
  return value;
}

这种写法会经常忘记返回jedis到pool.参考spting jdbctemplate的实现方式,优化如下

优化jedis使用方法

public static string getbytemplate(final string key) {
  redistemplate redistemplate = new redistemplate(getjedispool());
  string value = redistemplate.execute(new rediscallback<string>() {
    @override
    public string handle(jedis jedis) {
      return jedis.get(key);
    }
  });
  return value;
}

redistemplate封装了从jedispool中取jedis以及返回池中

public class redistemplate {

  private jedispool jedispool;

  public redistemplate(jedispool jedispool) {
    this.jedispool = jedispool;
  }

  public <t> t execute(rediscallback<t> callback) {
    jedis jedis = jedispool.getresource();
    try {
      return callback.handle(jedis);
    }
    catch (exception e) {
      // throw your exception
      throw e;
    }
    finally {
      returnresource(jedis);
    }
  }

  private void returnresource(jedis jedis) {
    if (jedis != null) {
      jedis.close();
    }
  }
}
public interface rediscallback<t> {
  public t handle(jedis jedis);
}

常用的jedis方法

字符串

@test
public void teststring() {
  jedis.set("name", "webb"); // 添加数据
  system.out.println("name -> " + jedis.get("name"));

  jedis.append("name", " , javaer"); // 拼接
  system.out.println("name -> " + jedis.get("name"));

  jedis.del("name"); // 删除数据
  system.out.println("name -> " + jedis.get("name"));

  jedis.mset("name", "webb", "age", "24"); // 设置多个键值对
  jedis.incr("age"); // 进行加1操作

  system.out.println("name -> " + jedis.get("name") + ", age -> " + jedis.get("age"));
}

列表

@test
public void testlist() {
  string key = "java framework";

  jedis.lpush(key, "spring");
  jedis.lpush(key, "spring mvc");
  jedis.lpush(key, "mybatis");

  system.out.println(jedis.lrange(key, 0 , -1)); // -1表示取得所有

  jedis.del(key);
  jedis.rpush(key, "spring");
  jedis.rpush(key, "spring mvc");
  jedis.rpush(key, "mybatis");

  system.out.println(jedis.lrange(key, 0 , -1)); // -1表示取得所有

  system.out.println(jedis.llen(key)); // 列表长度
  system.out.println(jedis.lrange(key, 0, 3));
  jedis.lset(key, 0 , "redis"); // 修改列表中单个值
  system.out.println(jedis.lindex(key, 1)); // 获取列表指定下标的值
  system.out.println(jedis.lpop(key)); // 列表出栈
  system.out.println(jedis.lrange(key, 0 , -1)); // -1表示取得所有
}

散列

@test
public void testmap() {
  string key = "user";

  map<string, string> map = new hashmap<>();
  map.put("name", "webb");
  map.put("age", "24");
  map.put("city", "hangzhou");

  jedis.hmset(key, map); // 添加数据

  list<string> rsmap = jedis.hmget(key, "name", "age", "city"); // 第一个参数存入的是redis中map对象的key,后面跟的是放入map中的对象的key
  system.out.println(rsmap);

  jedis.hdel(key, "age"); // 删除map中的某个键值

  system.out.println(jedis.hmget(key, "age"));
  system.out.println(jedis.hlen(key)); // 返回key为user的键中存放的值的个数
  system.out.println(jedis.exists(key)); // 是否存在key为user的记录
  system.out.println(jedis.hkeys(key)); // 返回map对象中的所有key
  system.out.println(jedis.hvals(key)); // 返回map对象中所有的value

  iterator<string> iterator = jedis.hkeys("user").iterator();

  while (iterator.hasnext()) {
    string key2 = iterator.next();
    system.out.print(key2 + " : " + jedis.hmget("user", key2) + "\n");
  }
}

集合

@test
public void testset() {
  string key = "userset";
  string key2 = "userset2";

  jedis.sadd(key, "webb");
  jedis.sadd(key, "webb");
  jedis.sadd(key, "lebo");
  jedis.sadd(key, "lebo0425");
  jedis.sadd(key, "who");

  jedis.srem(key, "who"); // 删除

  system.out.println(jedis.smembers(key)); // 获取所有加入的value
  system.out.println(jedis.sismember(key, "who")); // 判断value是否在集合中
  system.out.println(jedis.srandmember(key)); // 随机返回一个value
  system.out.println(jedis.scard(key)); // 返回集合的元素个数

  jedis.sadd(key2, "webb");
  jedis.sadd(key2, "ssq");

  system.out.println(jedis.sinter(key, key2)); // 交集
  system.out.println(jedis.sunion(key, key2)); // 并集
  system.out.println(jedis.sdiff(key, key2)); // 差集
}

有序集合

@test
public void testsortedset() {
  string key = "sortedset";

  jedis.zadd(key, 1999, "one");
  jedis.zadd(key, 1994, "two");
  jedis.zadd(key, 1998, "three");
  jedis.zadd(key, 2000, "four");
  jedis.zadd(key, 2017, "five");

  set<string> setvalues = jedis.zrange(key, 0 , -1); // score从小到大
  system.out.println(setvalues);
  set<string> setvalues2 = jedis.zrevrange(key, 0, -1); // score从大到小
  system.out.println(setvalues2);

  system.out.println(jedis.zcard(key)); // 元素个数
  system.out.println(jedis.zscore(key, "three")); // 元素下标
  system.out.println(jedis.zrange(key, 0, -1)); // 集合子集
  system.out.println(jedis.zrem(key, "five")); // 删除元素
  system.out.println(jedis.zcount(key, 1000, 2000)); // score在1000-2000内的元素个数
}

以上这篇详谈jedis连接池的使用就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。