吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 4828|回复: 14
收起左侧

[Java 转载] Java操作Redis之string讲解

[复制链接]
NullPointer 发表于 2016-11-18 17:39
说明:菜鸟一枚,正在学习Redis,没有底层的东西,只是一些命令的使用和讲解,希望能帮助到初学者。
一、Redis服务端:
  • package server;
  • import redis.clients.jedis.Jedis;
  • import redis.clients.jedis.JedisPool;
  • import redis.clients.jedis.JedisPoolConfig;
  • /**
  • * Redis的服务端
  • *
  • * @author TongWei.Chen
  • * @date 2016年11月18日11:57:44
  • */
  • public class RedisServer {
  •         //Redis服务器IP
  •         private static String REDIS_ADDR = "127.0.0.1";
  •         //Redis服务器端口号
  •         private static int REDIS_PORT = 6379;
  •         //可用连接实例的最大数目
  •         //若值为-1,则表示不限制。如果pool已经分配了maxActive个jedis实例,则此时pool的状态为exhusted(耗尽)
  •         private static int MAX_ACTIVE = -1;
  •         //控制一个pool中最多有多少个状态为idle(空闲的)Jedis实例,默认为8
  •         private static int MAX_IDLE = 100;
  •         //等待可用连接的最大时间,单位毫秒,默认值-1,表示永不超时,若超时,则抛出JedisConnectionException
  •         private static int MAX_WAIT = 1000 * 60 * 30;
  •         private static int TIME_OUT = 1000 * 60;
  •         //在选择一个Jedis实例时,是否提前进行valIDAte操作,若为true,则得到的Jedis实例都是可用的。
  •         private static boolean TEST_ON_BORROW = true;
  •        
  •         //Jedis实例
  •         private static JedisPool jedisPool = null;
  •        
  •         /**
  •          * 初始化连接池
  •          */
  •         static {
  •                 try {
  •                         JedisPoolConfig config = new JedisPoolConfig();
  •                         config.setMaxActive(MAX_ACTIVE);
  •                         config.setMaxIdle(MAX_IDLE);
  •                         config.setMaxActive(MAX_WAIT);
  •                         config.setTestOnBorrow(TEST_ON_BORROW);
  •                         jedisPool = new JedisPool(config, REDIS_ADDR, REDIS_PORT, TIME_OUT);
  •                 } catch (Exception e) {
  •                         e.printStackTrace();
  •                 }
  •         }
  •        
  •         /**
  •          * 获取Jedis实例
  •          * @return
  •          */
  •         public static Jedis getJedis() {
  •                 try {
  •                         if (null != jedisPool) {
  •                                 return jedisPool.getResource();
  •                         } else {
  •                                 return null;
  •                         }
  •                 } catch (Exception e) {
  •                         e.printStackTrace();
  •                         return null;
  •                 }
  •         }
  •        
  •         /**
  •          * 关闭Jedis,释放资源
  •          * @param jedis
  •          */
  •         public static void closeJedis(Jedis jedis) {
  •                 if(null != jedis) {
  •                         jedisPool.returnResource(jedis);
  •                 }
  •         }
  • }

二、Redis客户端

  • package client;
  • import redis.clients.jedis.Jedis;
  • import server.RedisServer;
  • /**
  • * Redis的客户端
  • *
  • * @author TongWei.Chen
  • * @date 2016年11月18日15:00:06
  • *
  • */
  • public class RedisClient {
  •        
  •         //获取Jedis实例
  •         private Jedis jedis = RedisServer.getJedis();
  •         /**
  •          * 默认构造器选择第一个数据库
  •          */
  •         public RedisClient() {
  •                 super();
  •                 jedis.select(0);
  •         }
  •        
  •         /**
  •          * 选择数据库
  •          * @param index
  •          */
  •         public void selectDB(int index) {
  •                 jedis.select(index);
  •         }
  •        
  •         /**
  •          * 清空所有的数据
  •          */
  •         public void flushDB() {
  •                 jedis.flushDB();
  •         }
  • }

三、string基本操作

  • package test;
  • import java.util.List;
  • import redis.clients.jedis.Jedis;
  • import server.RedisServer;
  • import client.RedisClient;
  • /**
  • * 测试Redis之string结构
  • *
  • * @author TongWei.Chen
  • * @date 2016年11月18日15:06:19
  • *
  • */
  • public class TestString {
  •         //获取Jedis实例
  •         private Jedis jedis = RedisServer.getJedis();
  •        
  •         /**
  •          * 设置key-value
  •          * @param key
  •          * @param value
  •          */
  •         public String set(String key, String value) {
  •                 return jedis.set(key, value);
  •         }
  •        
  •         /**
  •          * 根据key取value,没有对应的key则返回null
  •          * @param key
  •          */
  •         public String get(String key) {
  •                 return jedis.get(key);
  •         }
  •        
  •         /**
  •          * 根据key删除
  •          * @param key
  •          */
  •         public Long del(String ... key) {
  •                 return jedis.del(key);
  •         }
  •        
  •         /**
  •          * 返回key中字符串中的子串
  •          * 说明:
  •          * 【
  •          *         1.下标从0开始,若end超出了字符串总长度,则截取到最后一个元素
  •          *         2.若start为负数,则代表字符串从后往头开始,-5,-1代表从最后开始往头截取五个字符
  •          *  3.(0,-11)代表从第一个字符开始截取到从最后开始数到第11个字符结束。包含第11个
  •          * 】
  •          * @param key
  •          * @param start
  •          * @param end
  •          */
  •         public String getRange(String key, int start, int end) {
  •                 return jedis.getrange(key, start, end);
  •         }
  •        
  •         /**
  •          * 覆盖给定key所对应的字符串,覆盖的位置从偏移量 start 开始。返回被修改后的字符串的长度
  •          * @param key
  •          * @param start
  •          * @param value
  •          */
  •         public Long setRange(String key, long start, String value) {
  •                 return jedis.setrange(key, start, value);
  •         }
  •        
  •         /**
  •          * 设置key的值,返回旧值
  •          * 说明:
  •          * 【
  •          *         若key不存在,则返回null,但会创建此key
  •          * 】
  •          * @param key
  •          * @param value
  •          * @return
  •          */
  •         public String getSet(String key, String value) {
  •                 return jedis.getSet(key, value);
  •         }
  •        
  •         /**
  •          * 设置多个key-value,逗号隔开,返回ok
  •          * @param keysValues
  •          * @return
  •          */
  •         public String mSet(String ... keysValues) {
  •                 return jedis.mset(keysValues);
  •         }
  •        
  •         /**
  •          * 获取多个key的value,逗号隔开
  •          * @param keys
  •          * @return
  •          */
  •         public List<String> mGet(String ... keys) {
  •                 return jedis.mget(keys);
  •         }
  •        
  •         /**
  •          * 在key不存在的时候为key设置值,当key存在的时候不会覆盖现有value,这是与set命令最本质的区别
  •          * @param key
  •          * @param value
  •          * @Return 0:error 1:ok
  •          */
  •         public Long setNx(String key, String value) {
  •                 return jedis.setnx(key, value);
  •         }
  •        
  •         /**
  •          * 设置多个key-value,在key不存在的时候为key设置值,当key存在的时候不会覆盖现有value,逗号隔开
  •          * 说明:
  •          * 【
  •          *         1.当有1个key存在时,则直接返回0,所有的key都无法设置。
  •          * 】
  •          * @param keysValues
  •          * @return 0:error 1:ok
  •          */
  •         public Long mSetNx(String ... keysValues) {
  •                 return jedis.msetnx(keysValues);
  •         }
  •        
  •         /**
  •          * 设置带有时效性的key-value,若key已存在,则会覆盖,并创建时效性。到设置的秒数自动删除key-value
  •          * @param key
  •          * @param seconds:秒
  •          * @param value
  •          * @return
  •          */
  •         public String setEx(String key, int seconds, String value) {
  •                 return jedis.setex(key, seconds, value);
  •         }
  •        
  •         /**
  •          * 追加字符串
  •          * 说明:
  •          * 【
  •          *         当有key不存在时,相当于执行set key value操作
  •          * 】
  •          * @return 追加后的字符串长度
  •          */
  •         public Long append(String key, String value) {
  •                 return jedis.append(key, value);
  •         }
  •        
  •         /**
  •          * 返回key对应的字符串的长度,若key不存在则返回0
  •          * @param key
  •          * @return
  •          */
  •         public Long strLen(String key) {
  •                 return jedis.strlen(key);
  •         }
  •         /**
  •          * 对value进行++操作
  •          * 说明:
  •          * 【
  •          *         1.当有key不存在时,相当于执行set key 0操作,自动为key默认为0,在执行++操作
  •          *  2.当key存在时,进行key ++操作
  •          *  3.如果值包含错误的类型,或字符串类型的值不能表示为数字,那么返回一个错误。
  •          * 】
  •          * @param key
  •          * @return
  •          */
  •         public Long incr(String key) {
  •                 return jedis.incr(key);
  •         }
  •         /**
  •          * 对value进行+num操作
  •          * 说明:
  •          * 【
  •          *         1.当有key不存在时,相当于执行set key 0操作,自动为key默认为0,在执行+num操作
  •          *  2.当key存在时,进行key +num操作
  •          *  3.如果值包含错误的类型,或字符串类型的值不能表示为数字,那么返回一个错误。
  •          * 】
  •          * @param key
  •          * @return
  •          */
  •         public Long incrBy(String key, int num) {
  •                 return jedis.incrBy(key, num);
  •         }
  •        
  •         /**
  •          * 对value进行--操作
  •          * 说明:
  •          * 【
  •          *         1.当有key不存在时,相当于执行set key 0操作,自动为key默认为0,在执行--操作
  •          *  2.当key存在时,进行key --操作
  •          *  3.如果值包含错误的类型,或字符串类型的值不能表示为数字,那么返回一个错误。
  •          * 】
  •          * @param key
  •          * @return
  •          */
  •         public Long decr(String key) {
  •                 return jedis.decr(key);
  •         }
  •         /**
  •          * 对value进行-num操作
  •          * 说明:
  •          * 【
  •          *         1.当有key不存在时,相当于执行set key 0操作,自动为key默认为0,在执行-num操作
  •          *  2.当key存在时,进行key -num操作
  •          *  3.如果值包含错误的类型,或字符串类型的值不能表示为数字,那么返回一个错误。
  •          * 】
  •          * @param key
  •          * @return
  •          */
  •         public Long decrBy(String key, int num) {
  •                 return jedis.decrBy(key, num);
  •         }
  •        
  •         public static void main(String[] args) {
  •                 //选择db
  •                 RedisClient redisClient = new RedisClient();
  •                 TestString test = new TestString();
  • //                test.set("name", "chentw@163.com");
  • //                System.out.println(test.get("name"));
  • //                test.del("name");
  • //                System.out.println(test.getRange("name", 0, -11));
  • //                System.out.println(test.getRange("name", -5, -1));
  • //                System.out.println(test.setRange("key1", 5, "value"));
  • //                System.out.println(test.getSet("name", "TongWei.Chen@163.com"));
  • //                System.out.println(test.mSet("key1", "value1", "key2", "value2", "key3", "value3"));
  • //                System.out.println(test.mGet("key1", "key2"));
  • //                System.out.println(test.setNx("key11", "value11"));
  • //                System.out.println(test.setEx("key1", 10, "value"));
  • //                System.out.println(test.strLen("key1"));
  • //                System.out.println(test.mSetNx("k1", "v1", "k2", "v2", "name", "nameValue"));
  • //                System.out.println(test.append("key1", "test"));
  • //                System.out.println(test.incr("age"));
  • //                System.out.println(test.incrBy("age", -10));
  • //                System.out.println(test.decr("age"));
  • //                System.out.println(test.decrBy("age", -10));
  •         }
  • }



免费评分

参与人数 2热心值 +2 收起 理由
tiancaizaizuo + 1 必须赞一个
wwwmaopu1201 + 1 大哥发帖,必须赞一个

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

ShadowY 发表于 2016-11-18 17:55
楼主厉害!顶顶
 楼主| NullPointer 发表于 2016-11-18 17:57
ShadowY 发表于 2016-11-18 17:59
头像被屏蔽
总有刁民想阴朕 发表于 2016-11-18 18:41
提示: 作者被禁止或删除 内容自动屏蔽
沐易永 发表于 2016-11-18 18:59
支持楼主!!!!
不拼~怎么赢 发表于 2016-11-18 19:58
一起努力
wwwmaopu1201 发表于 2016-11-18 22:38
必须赞一个
tiancaizaizuo 发表于 2016-11-18 22:39
干货  必须赞一个
2909094965 发表于 2016-11-19 01:38 来自手机
谢谢分享 ,值得学习
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-5-10 23:33

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表