redis操作String

继续接上篇:redis操作key

这个没什么多说的就直接上源码,不懂可以留言请教,谢谢!

  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("======================String_1==========================");
  37.         // 清空数据 
  38.         System.out.println("清空库中所有数据:"+jedis.flushDB());
  39.         System.out.println("=============增=============");
  40.         jedis.set("key001","value001");
  41.         jedis.set("key002","value002");
  42.         jedis.set("key003","value003");
  43.         System.out.println("已新增的3个键值对如下:");
  44.         System.out.println(jedis.get("key001"));
  45.         System.out.println(jedis.get("key002"));
  46.         System.out.println(jedis.get("key003"));
  47.         System.out.println("=============删=============");
  48.         System.out.println("删除key003键值对:"+jedis.del("key003"));
  49.         System.out.println("获取key003键对应的值:"+jedis.get("key003"));
  50.         System.out.println("=============改=============");
  51.         //1、直接覆盖原来的数据
  52.         System.out.println("直接覆盖key001原来的数据:"+jedis.set("key001","value001-update"));
  53.         System.out.println("获取key001对应的新值:"+jedis.get("key001"));
  54.         //2、直接覆盖原来的数据  
  55.         System.out.println("在key002原来值后面追加:"+jedis.append("key002","+appendString"));
  56.         System.out.println("获取key002对应的新值"+jedis.get("key002"));
  57.         System.out.println("=============增,删,查(多个)=============");
  58.         /** 
  59.          * mset,mget同时新增,修改,查询多个键值对 
  60.          * 等价于:
  61.          * jedis.set("name","ssss"); 
  62.          * jedis.set("jarorwar","xxxx"); 
  63.          */
  64.         System.out.println("一次性新增key201,key202,key203,key204及其对应值:"+jedis.mset("key201","value201",
  65.                         "key202","value202","key203","value203","key204","value204"));
  66.         System.out.println("一次性获取key201,key202,key203,key204各自对应的值:"+
  67.                         jedis.mget("key201","key202","key203","key204"));
  68.         System.out.println("一次性删除key201,key202:"+jedis.del(new String[]{"key201""key202"}));
  69.         System.out.println("一次性获取key201,key202,key203,key204各自对应的值:"+
  70.                 jedis.mget("key201","key202","key203","key204"));
  71.         System.out.println();
  72.         //jedis具备的功能shardedJedis中也可直接使用,下面测试一些前面没用过的方法
  73.         System.out.println("======================String_2==========================");
  74.         // 清空数据 
  75.         System.out.println("清空库中所有数据:"+jedis.flushDB());
  76.         System.out.println("=============新增键值对时防止覆盖原先值=============");
  77.         System.out.println("原先key301不存在时,新增key301:"+jedis.setnx("key301""value301"));
  78.         System.out.println("原先key302不存在时,新增key302:"+jedis.setnx("key302""value302"));
  79.         System.out.println("当key302存在时,尝试新增key302:"+jedis.setnx("key302""value302_new"));
  80.         System.out.println("获取key301对应的值:"+jedis.get("key301"));
  81.         System.out.println("获取key302对应的值:"+jedis.get("key302"));
  82.         System.out.println("=============超过有效期键值对被删除=============");
  83.         // 设置key的有效期,并存储数据 
  84.         System.out.println("新增key303,并指定过期时间为2秒"+jedis.setex("key303"2"key303-2second"));
  85.         System.out.println("获取key303对应的值:"+jedis.get("key303"));
  86.         try{
  87.             Thread.sleep(3000);
  88.         }
  89.         catch (InterruptedException e){
  90.         }
  91.         System.out.println("3秒之后,获取key303对应的值:"+jedis.get("key303"));
  92.         System.out.println("=============获取原值,更新为新值一步完成=============");
  93.         System.out.println("key302原值:"+jedis.getSet("key302""value302-after-getset"));
  94.         System.out.println("key302新值:"+jedis.get("key302"));
  95.         System.out.println("=============获取子串=============");
  96.         System.out.println("获取key302对应值中的子串:"+jedis.getrange("key302"57));
  97.     }
  98. }

redis操作String

未完待续...

下篇传送阵redis操作List

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

支付宝扫一扫赞助

微信钱包扫描赞助

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

发表评论

gravatar

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

目前评论:6   其中:访客  4   博主  2

  1. avatar 天空博客 2

    博主你要是能写一个完整的案例出来就好

    • avatar 小C

      @天空博客 讲讲基础算了,完整的这个涉及的有点多,我也是刚入门!!

  2. avatar 好文推荐 4

    赞一个

  3. avatar 大学问社区 3

    专业

  4. avatar 成都SEO 3

    这是什么鬼?

评论加载中...