redis操作hash

继续接上篇:redis操作zset

不想做过多说明直接上代码吧!

  1. public class RedisUtil {
  2.     /*
  3.      * 连接redis
  4.      */
  5.     public static JedisPool buildJedisPool() throws Exception {
  6.         JedisPoolConfig config = new JedisPoolConfig();
  7.         JedisPool jedisPool = null;
  8.         try {
  9.             config.setMinIdle(50);
  10.             config.setMaxIdle(3000);
  11.             jedisPool = new JedisPool(config, "localhost"6379);
  12.             return jedisPool;
  13.         } catch (Exception e) {
  14.             e.printStackTrace();
  15.         }
  16.         return jedisPool;
  17.     }
  18.     /*
  19.      * 关闭redis
  20.      */
  21.     public static void closeConnection(Jedis jedis) throws Exception {
  22.         try {
  23.             if (null != jedis) {
  24.                 jedis.disconnect();
  25.             }
  26.         } catch (Exception e) {
  27.             e.printStackTrace();
  28.         }
  29.     }
  30.     /*
  31.      * 测试
  32.      */
  33.     public static void main(String[] args) throws Exception {
  34.         Jedis jedis = buildJedisPool().getResource();//连接redis
  35.         System.out.println("清空库中所有数据:"+jedis.flushDB());
  36.         System.out.println("======================hash==========================");
  37.         //清空数据 
  38.         System.out.println(jedis.flushDB());
  39.         System.out.println("=============增=============");
  40.         System.out.println("hashs中添加key001和value001键值对:"+jedis.hset("hashs""key001""value001"));
  41.         System.out.println("hashs中添加key002和value002键值对:"+jedis.hset("hashs""key002""value002"));
  42.         System.out.println("hashs中添加key003和value003键值对:"+jedis.hset("hashs""key003""value003"));
  43.         System.out.println("新增key004和4的整型键值对:"+jedis.hincrBy("hashs""key004", 4l));
  44.         System.out.println("hashs中的所有值:"+jedis.hvals("hashs"));
  45.         System.out.println();
  46.         System.out.println("=============删=============");
  47.         System.out.println("hashs中删除key002键值对:"+jedis.hdel("hashs""key002"));
  48.         System.out.println("hashs中的所有值:"+jedis.hvals("hashs"));
  49.         System.out.println();
  50.         System.out.println("=============改=============");
  51.         System.out.println("key004整型键值的值增加100:"+jedis.hincrBy("hashs""key004", 100l));
  52.         System.out.println("hashs中的所有值:"+jedis.hvals("hashs"));
  53.         System.out.println();
  54.         System.out.println("=============查=============");
  55.         System.out.println("判断key003是否存在:"+jedis.hexists("hashs""key003"));
  56.         System.out.println("获取key004对应的值:"+jedis.hget("hashs""key004"));
  57.         System.out.println("批量获取key001和key003对应的值:"+jedis.hmget("hashs""key001""key003"));
  58.         System.out.println("获取hashs中所有的key:"+jedis.hkeys("hashs"));
  59.         System.out.println("获取hashs中所有的value:"+jedis.hvals("hashs"));
  60.     }
  61. }

redis操作hash

未完待续...

下篇传送阵:Redis常用命令

您可以选择一种方式赞助本站

支付宝扫一扫赞助

微信钱包扫描赞助

    A+
发布日期:2016年04月25日  所属分类:Java
标签:

发表评论

gravatar

:?::razz::sad::evil::!::smile::oops::grin::eek::shock::???::cool::lol::mad::twisted::roll::wink::idea::arrow::neutral::cry::mrgreen:

目前评论:1   其中:访客  1   博主  0

  1. avatar 大城小我 5

    小c同学老是发大城小我不懂的干货,叫我情何以堪 :grin:

评论加载中...