Java中操作Redis
一、server 端安装
1、下载
https://github.com/MSOpenTech/redis
可看到当前可下载版本:redis2.6
下载后的文件为:
解压后,选择当前 64 位 win7 系统对应的版本:
2、安装
1)解压后将里面所有文件拷贝至 redis 安装目录:
几个 exe 程序的功能:
redis-benchmark.exe:性能测试,用以模拟同时由 N 个客户端发送 M 个 SETs/GETs 查询 (类似于 Apache 的 ab 工具).
redis-check-aof.exe:更新日志检查
redis-check-dump.exe:本地数据库检查
redis-server.exe:服务端
2)将路径添加至系统环境变量:过程略
3)cmd 下启动 redis-server
3、客户端访问测试
另起一个 cmd 窗口:
二、基本功能测试
1、程序基本结构
2、主要类
1)功能类
package com.redis;import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisShardInfo;
import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;
import redis.clients.jedis.SortingParams;public class RedisClient {
</span><span style="color: rgba(0, 0, 255, 1)">private</span> Jedis jedis;<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">非切片额客户端连接</span> <span style="color: rgba(0, 0, 255, 1)">private</span> JedisPool jedisPool;<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">非切片连接池</span> <span style="color: rgba(0, 0, 255, 1)">private</span> ShardedJedis shardedJedis;<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">切片额客户端连接</span> <span style="color: rgba(0, 0, 255, 1)">private</span> ShardedJedisPool shardedJedisPool;<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">切片连接池</span> <span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> RedisClient() { initialPool(); initialShardedPool(); shardedJedis </span>=<span style="color: rgba(0, 0, 0, 1)"> shardedJedisPool.getResource(); jedis </span>=<span style="color: rgba(0, 0, 0, 1)"> jedisPool.getResource(); } </span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)"> * 初始化非切片池 </span><span style="color: rgba(0, 128, 0, 1)">*/</span> <span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> initialPool() { </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 池基本配置 </span> JedisPoolConfig config = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> JedisPoolConfig(); config.setMaxActive(</span>20<span style="color: rgba(0, 0, 0, 1)">); config.setMaxIdle(</span>5<span style="color: rgba(0, 0, 0, 1)">); config.setMaxWait(</span>1000l<span style="color: rgba(0, 0, 0, 1)">); config.setTestOnBorrow(</span><span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">); jedisPool </span>= <span style="color: rgba(0, 0, 255, 1)">new</span> JedisPool(config,"127.0.0.1",6379<span style="color: rgba(0, 0, 0, 1)">); } </span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)"> * 初始化切片池 </span><span style="color: rgba(0, 128, 0, 1)">*/</span> <span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> initialShardedPool() { </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 池基本配置 </span> JedisPoolConfig config = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> JedisPoolConfig(); config.setMaxActive(</span>20<span style="color: rgba(0, 0, 0, 1)">); config.setMaxIdle(</span>5<span style="color: rgba(0, 0, 0, 1)">); config.setMaxWait(</span>1000l<span style="color: rgba(0, 0, 0, 1)">); config.setTestOnBorrow(</span><span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> slave链接 </span> List<JedisShardInfo> shards = <span style="color: rgba(0, 0, 255, 1)">new</span> ArrayList<JedisShardInfo><span style="color: rgba(0, 0, 0, 1)">(); shards.add(</span><span style="color: rgba(0, 0, 255, 1)">new</span> JedisShardInfo("127.0.0.1", 6379, "master"<span style="color: rgba(0, 0, 0, 1)">)); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 构造池 </span> shardedJedisPool = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ShardedJedisPool(config, shards); } </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> show() { KeyOperate(); StringOperate(); ListOperate(); SetOperate(); SortedSetOperate(); HashOperate(); jedisPool.returnResource(jedis); shardedJedisPool.returnResource(shardedJedis); } </span><span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> KeyOperate() { 。。。 } </span><span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> StringOperate() { 。。。 } </span><span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> ListOperate() { 。。。 } </span><span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> SetOperate() { 。。。 } </span><span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> SortedSetOperate() { 。。。 } </span><span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> HashOperate() { 。。。 }
}
2)测试类
package com.redis;public class Main {
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> main(String[] args) { </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> TODO Auto-generated method stub</span> <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> RedisClient().show(); }
}
3、各个功能函数
1)key 功能
private void KeyOperate() { System.out.println("======================key=========================="); // 清空数据 System.out.println("清空库中所有数据:"+jedis.flushDB()); // 判断 key 否存在 System.out.println("判断 key999 键是否存在:"+shardedJedis.exists("key999")); System.out.println("新增 key001,value001 键值对:"+shardedJedis.set("key001", "value001")); System.out.println("判断 key001 是否存在:"+shardedJedis.exists("key001")); // 输出系统中所有的 key System.out.println("新增 key002,value002 键值对:"+shardedJedis.set("key002", "value002")); System.out.println("系统中所有键如下:"); Set<String> keys = jedis.keys("*"); Iterator<String> it=keys.iterator() ; while(it.hasNext()){ String key = it.next(); System.out.println(key); } // 删除某个 key, 若 key 不存在,则忽略该命令。 System.out.println("系统中删除 key002:"+jedis.del("key002")); System.out.println("判断 key002 是否存在:"+shardedJedis.exists("key002")); // 设置 key001 的过期时间 System.out.println("设置 key001 的过期时间为 5 秒:"+jedis.expire("key001", 5)); try{ Thread.sleep(2000);} catch (InterruptedException e){ } // 查看某个 key 的剩余生存时间, 单位【秒】. 永久生存或者不存在的都返回 -1 System.out.println("查看 key001 的剩余生存时间:"+jedis.ttl("key001")); // 移除某个 key 的生存时间 System.out.println("移除 key001 的生存时间:"+jedis.persist("key001")); System.out.println("查看 key001 的剩余生存时间:"+jedis.ttl("key001")); // 查看 key 所储存的值的类型 System.out.println("查看 key 所储存的值的类型:"+jedis.type("key001")); /* * 一些其他方法:1、修改键名:jedis.rename("key6", "key0"); * 2、将当前 db 的 key 移动到给定的 db 当中:jedis.move("foo", 1) */ }
运行结果:
======================key========================== 清空库中所有数据:OK 判断 key999 键是否存在:false 新增 key001,value001 键值对:OK 判断 key001 是否存在:true 新增 key002,value002 键值对:OK 系统中所有键如下: key002 key001 系统中删除 key002: 1 判断 key002 是否存在:false 设置 key001 的过期时间为 5 秒:1 查看 key001 的剩余生存时间:3 移除 key001 的生存时间:1 查看 key001 的剩余生存时间:-1 查看 key 所储存的值的类型:string
2)String 功能
private void StringOperate() { System.out.println("======================String_1=========================="); // 清空数据 System.out.println("清空库中所有数据:"+jedis.flushDB());System.out.println(</span>"=============增============="<span style="color: rgba(0, 0, 0, 1)">); jedis.set(</span>"key001","value001"<span style="color: rgba(0, 0, 0, 1)">); jedis.set(</span>"key002","value002"<span style="color: rgba(0, 0, 0, 1)">); jedis.set(</span>"key003","value003"<span style="color: rgba(0, 0, 0, 1)">); System.out.println(</span>"已新增的3个键值对如下:"<span style="color: rgba(0, 0, 0, 1)">); System.out.println(jedis.get(</span>"key001"<span style="color: rgba(0, 0, 0, 1)">)); System.out.println(jedis.get(</span>"key002"<span style="color: rgba(0, 0, 0, 1)">)); System.out.println(jedis.get(</span>"key003"<span style="color: rgba(0, 0, 0, 1)">)); System.out.println(</span>"=============删============="<span style="color: rgba(0, 0, 0, 1)">); System.out.println(</span>"删除key003键值对:"+jedis.del("key003"<span style="color: rgba(0, 0, 0, 1)">)); System.out.println(</span>"获取key003键对应的值:"+jedis.get("key003"<span style="color: rgba(0, 0, 0, 1)">)); System.out.println(</span>"=============改============="<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">1、直接覆盖原来的数据</span> System.out.println("直接覆盖key001原来的数据:"+jedis.set("key001","value001-update"<span style="color: rgba(0, 0, 0, 1)">)); System.out.println(</span>"获取key001对应的新值:"+jedis.get("key001"<span style="color: rgba(0, 0, 0, 1)">)); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">2、直接覆盖原来的数据 </span> System.out.println("在key002原来值后面追加:"+jedis.append("key002","+appendString"<span style="color: rgba(0, 0, 0, 1)">)); System.out.println(</span>"获取key002对应的新值"+jedis.get("key002"<span style="color: rgba(0, 0, 0, 1)">)); System.out.println(</span>"=============增,删,查(多个)============="<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)"> * mset,mget同时新增,修改,查询多个键值对 * 等价于: * jedis.set("name","ssss"); * jedis.set("jarorwar","xxxx"); </span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)"> System.out.println(</span>"一次性新增key201,key202,key203,key204及其对应值:"+jedis.mset("key201","value201"<span style="color: rgba(0, 0, 0, 1)">, </span>"key202","value202","key203","value203","key204","value204"<span style="color: rgba(0, 0, 0, 1)">)); System.out.println(</span>"一次性获取key201,key202,key203,key204各自对应的值:"+<span style="color: rgba(0, 0, 0, 1)"> jedis.mget(</span>"key201","key202","key203","key204"<span style="color: rgba(0, 0, 0, 1)">)); System.out.println(</span>"一次性删除key201,key202:"+jedis.del(<span style="color: rgba(0, 0, 255, 1)">new</span> String[]{"key201", "key202"<span style="color: rgba(0, 0, 0, 1)">})); System.out.println(</span>"一次性获取key201,key202,key203,key204各自对应的值:"+<span style="color: rgba(0, 0, 0, 1)"> jedis.mget(</span>"key201","key202","key203","key204"<span style="color: rgba(0, 0, 0, 1)">)); System.out.println(); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">jedis具备的功能shardedJedis中也可直接使用,下面测试一些前面没用过的方法</span> System.out.println("======================String_2=========================="<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 清空数据 </span> System.out.println("清空库中所有数据:"+<span style="color: rgba(0, 0, 0, 1)">jedis.flushDB()); System.out.println(</span>"=============新增键值对时防止覆盖原先值============="<span style="color: rgba(0, 0, 0, 1)">); System.out.println(</span>"原先key301不存在时,新增key301:"+shardedJedis.setnx("key301", "value301"<span style="color: rgba(0, 0, 0, 1)">)); System.out.println(</span>"原先key302不存在时,新增key302:"+shardedJedis.setnx("key302", "value302"<span style="color: rgba(0, 0, 0, 1)">)); System.out.println(</span>"当key302存在时,尝试新增key302:"+shardedJedis.setnx("key302", "value302_new"<span style="color: rgba(0, 0, 0, 1)">)); System.out.println(</span>"获取key301对应的值:"+shardedJedis.get("key301"<span style="color: rgba(0, 0, 0, 1)">)); System.out.println(</span>"获取key302对应的值:"+shardedJedis.get("key302"<span style="color: rgba(0, 0, 0, 1)">)); System.out.println(</span>"=============超过有效期键值对被删除============="<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 设置key的有效期,并存储数据 </span> System.out.println("新增key303,并指定过期时间为2秒"+shardedJedis.setex("key303", 2, "key303-2second"<span style="color: rgba(0, 0, 0, 1)">)); System.out.println(</span>"获取key303对应的值:"+shardedJedis.get("key303"<span style="color: rgba(0, 0, 0, 1)">)); </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)">{ Thread.sleep(</span>3000<span style="color: rgba(0, 0, 0, 1)">); } </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (InterruptedException e){ } System.out.println(</span>"3秒之后,获取key303对应的值:"+shardedJedis.get("key303"<span style="color: rgba(0, 0, 0, 1)">)); System.out.println(</span>"=============获取原值,更新为新值一步完成============="<span style="color: rgba(0, 0, 0, 1)">); System.out.println(</span>"key302原值:"+shardedJedis.getSet("key302", "value302-after-getset"<span style="color: rgba(0, 0, 0, 1)">)); System.out.println(</span>"key302新值:"+shardedJedis.get("key302"<span style="color: rgba(0, 0, 0, 1)">)); System.out.println(</span>"=============获取子串============="<span style="color: rgba(0, 0, 0, 1)">); System.out.println(</span>"获取key302对应值中的子串:"+shardedJedis.getrange("key302", 5, 7<span style="color: rgba(0, 0, 0, 1)">)); }</span></pre>
运行结果:
======================String_1========================== 清空库中所有数据:OK ============= 增 ============= 已新增的 3 个键值对如下: value001 value002 value003 ============= 删 ============= 删除 key003 键值对:1 获取 key003 键对应的值:null ============= 改 ============= 直接覆盖 key001 原来的数据:OK 获取 key001 对应的新值:value001-update 在 key002 原来值后面追加:21 获取 key002 对应的新值 value002+appendString ============= 增,删,查(多个)============= 一次性新增 key201,key202,key203,key204 及其对应值:OK 一次性获取 key201,key202,key203,key204 各自对应的值:[value201, value202, value203, value204] 一次性删除 key201,key202:2 一次性获取 key201,key202,key203,key204 各自对应的值:[null, null, value203, value204]======================String_2==========================
清空库中所有数据:OK
============= 新增键值对时防止覆盖原先值 =============
原先 key301 不存在时,新增 key301:1
原先 key302 不存在时,新增 key302:1
当 key302 存在时,尝试新增 key302:0
获取 key301 对应的值:value301
获取 key302 对应的值:value302
============= 超过有效期键值对被删除 =============
新增 key303,并指定过期时间为 2 秒 OK
获取 key303 对应的值:key303-2second
3 秒之后,获取 key303 对应的值:null
============= 获取原值,更新为新值一步完成 =============
key302 原值:value302
key302 新值:value302-after-getset
============= 获取子串 =============
获取 key302 对应值中的子串:302
3)List 功能
private void ListOperate() { System.out.println("======================list=========================="); // 清空数据 System.out.println("清空库中所有数据:"+jedis.flushDB());System.out.println(</span>"=============增============="<span style="color: rgba(0, 0, 0, 1)">); shardedJedis.lpush(</span>"stringlists", "vector"<span style="color: rgba(0, 0, 0, 1)">); shardedJedis.lpush(</span>"stringlists", "ArrayList"<span style="color: rgba(0, 0, 0, 1)">); shardedJedis.lpush(</span>"stringlists", "vector"<span style="color: rgba(0, 0, 0, 1)">); shardedJedis.lpush(</span>"stringlists", "vector"<span style="color: rgba(0, 0, 0, 1)">); shardedJedis.lpush(</span>"stringlists", "LinkedList"<span style="color: rgba(0, 0, 0, 1)">); shardedJedis.lpush(</span>"stringlists", "MapList"<span style="color: rgba(0, 0, 0, 1)">); shardedJedis.lpush(</span>"stringlists", "SerialList"<span style="color: rgba(0, 0, 0, 1)">); shardedJedis.lpush(</span>"stringlists", "HashList"<span style="color: rgba(0, 0, 0, 1)">); shardedJedis.lpush(</span>"numberlists", "3"<span style="color: rgba(0, 0, 0, 1)">); shardedJedis.lpush(</span>"numberlists", "1"<span style="color: rgba(0, 0, 0, 1)">); shardedJedis.lpush(</span>"numberlists", "5"<span style="color: rgba(0, 0, 0, 1)">); shardedJedis.lpush(</span>"numberlists", "2"<span style="color: rgba(0, 0, 0, 1)">); System.out.println(</span>"所有元素-stringlists:"+shardedJedis.lrange("stringlists", 0, -1<span style="color: rgba(0, 0, 0, 1)">)); System.out.println(</span>"所有元素-numberlists:"+shardedJedis.lrange("numberlists", 0, -1<span style="color: rgba(0, 0, 0, 1)">)); System.out.println(</span>"=============删============="<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 删除列表指定的值 ,第二个参数为删除的个数(有重复时),后add进去的值先被删,类似于出栈</span> System.out.println("成功删除指定元素个数-stringlists:"+shardedJedis.lrem("stringlists", 2, "vector"<span style="color: rgba(0, 0, 0, 1)">)); System.out.println(</span>"删除指定元素之后-stringlists:"+shardedJedis.lrange("stringlists", 0, -1<span style="color: rgba(0, 0, 0, 1)">)); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 删除区间以外的数据 </span> System.out.println("删除下标0-3区间之外的元素:"+shardedJedis.ltrim("stringlists", 0, 3<span style="color: rgba(0, 0, 0, 1)">)); System.out.println(</span>"删除指定区间之外元素后-stringlists:"+shardedJedis.lrange("stringlists", 0, -1<span style="color: rgba(0, 0, 0, 1)">)); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 列表元素出栈 </span> System.out.println("出栈元素:"+shardedJedis.lpop("stringlists"<span style="color: rgba(0, 0, 0, 1)">)); System.out.println(</span>"元素出栈后-stringlists:"+shardedJedis.lrange("stringlists", 0, -1<span style="color: rgba(0, 0, 0, 1)">)); System.out.println(</span>"=============改============="<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 修改列表中指定下标的值 </span> shardedJedis.lset("stringlists", 0, "hello list!"<span style="color: rgba(0, 0, 0, 1)">); System.out.println(</span>"下标为0的值修改后-stringlists:"+shardedJedis.lrange("stringlists", 0, -1<span style="color: rgba(0, 0, 0, 1)">)); System.out.println(</span>"=============查============="<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 数组长度 </span> System.out.println("长度-stringlists:"+shardedJedis.llen("stringlists"<span style="color: rgba(0, 0, 0, 1)">)); System.out.println(</span>"长度-numberlists:"+shardedJedis.llen("numberlists"<span style="color: rgba(0, 0, 0, 1)">)); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 排序 </span> <span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)"> * list中存字符串时必须指定参数为alpha,如果不使用SortingParams,而是直接使用sort("list"), * 会出现"ERR One or more scores can't be converted into double" </span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)"> SortingParams sortingParameters </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> SortingParams(); sortingParameters.alpha(); sortingParameters.limit(</span>0, 3<span style="color: rgba(0, 0, 0, 1)">); System.out.println(</span>"返回排序后的结果-stringlists:"+shardedJedis.sort("stringlists"<span style="color: rgba(0, 0, 0, 1)">,sortingParameters)); System.out.println(</span>"返回排序后的结果-numberlists:"+shardedJedis.sort("numberlists"<span style="color: rgba(0, 0, 0, 1)">)); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 子串: start为元素下标,end也为元素下标;-1代表倒数一个元素,-2代表倒数第二个元素</span> System.out.println("子串-第二个开始到结束:"+shardedJedis.lrange("stringlists", 1, -1<span style="color: rgba(0, 0, 0, 1)">)); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 获取列表指定下标的值 </span> System.out.println("获取下标为2的元素:"+shardedJedis.lindex("stringlists", 2)+"\n"<span style="color: rgba(0, 0, 0, 1)">); }</span></pre>
运行结果:
======================list========================== 清空库中所有数据:OK ============= 增 ============= 所有元素-stringlists:[HashList, SerialList, MapList, LinkedList, vector, vector, ArrayList, vector] 所有元素-numberlists:[2, 5, 1, 3] ============= 删 ============= 成功删除指定元素个数-stringlists:2 删除指定元素之后-stringlists:[HashList, SerialList, MapList, LinkedList, ArrayList, vector] 删除下标 0-3 区间之外的元素:OK 删除指定区间之外元素后-stringlists:[HashList, SerialList, MapList, LinkedList] 出栈元素:HashList 元素出栈后-stringlists:[SerialList, MapList, LinkedList] ============= 改 ============= 下标为 0 的值修改后-stringlists:[hello list!, MapList, LinkedList] ============= 查 ============= 长度-stringlists:3 长度-numberlists:4 返回排序后的结果-stringlists:[LinkedList, MapList, hello list!] 返回排序后的结果-numberlists:[1, 2, 3, 5] 子串-第二个开始到结束:[MapList, LinkedList] 获取下标为 2 的元素:LinkedList
4)Set 功能
private void SetOperate() {}System.out.println(</span>"======================set=========================="<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 清空数据 </span> System.out.println("清空库中所有数据:"+<span style="color: rgba(0, 0, 0, 1)">jedis.flushDB()); System.out.println(</span>"=============增============="<span style="color: rgba(0, 0, 0, 1)">); System.out.println(</span>"向sets集合中加入元素element001:"+jedis.sadd("sets", "element001"<span style="color: rgba(0, 0, 0, 1)">)); System.out.println(</span>"向sets集合中加入元素element002:"+jedis.sadd("sets", "element002"<span style="color: rgba(0, 0, 0, 1)">)); System.out.println(</span>"向sets集合中加入元素element003:"+jedis.sadd("sets", "element003"<span style="color: rgba(0, 0, 0, 1)">)); System.out.println(</span>"向sets集合中加入元素element004:"+jedis.sadd("sets", "element004"<span style="color: rgba(0, 0, 0, 1)">)); System.out.println(</span>"查看sets集合中的所有元素:"+jedis.smembers("sets"<span style="color: rgba(0, 0, 0, 1)">)); System.out.println(); System.out.println(</span>"=============删============="<span style="color: rgba(0, 0, 0, 1)">); System.out.println(</span>"集合sets中删除元素element003:"+jedis.srem("sets", "element003"<span style="color: rgba(0, 0, 0, 1)">)); System.out.println(</span>"查看sets集合中的所有元素:"+jedis.smembers("sets"<span style="color: rgba(0, 0, 0, 1)">)); </span><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">System.out.println("sets集合中任意位置的元素出栈:"+jedis.spop("sets"));//注:出栈元素位置居然不定?--无实际意义 System.out.println("查看sets集合中的所有元素:"+jedis.smembers("sets"));</span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)"> System.out.println(); System.out.println(</span>"=============改============="<span style="color: rgba(0, 0, 0, 1)">); System.out.println(); System.out.println(</span>"=============查============="<span style="color: rgba(0, 0, 0, 1)">); System.out.println(</span>"判断element001是否在集合sets中:"+jedis.sismember("sets", "element001"<span style="color: rgba(0, 0, 0, 1)">)); System.out.println(</span>"循环查询获取sets中的每个元素:"<span style="color: rgba(0, 0, 0, 1)">); Set</span><String> set = jedis.smembers("sets"<span style="color: rgba(0, 0, 0, 1)">); Iterator</span><String> it=<span style="color: rgba(0, 0, 0, 1)">set.iterator() ; </span><span style="color: rgba(0, 0, 255, 1)">while</span><span style="color: rgba(0, 0, 0, 1)">(it.hasNext()){ Object obj</span>=<span style="color: rgba(0, 0, 0, 1)">it.next(); System.out.println(obj); } System.out.println(); System.out.println(</span>"=============集合运算============="<span style="color: rgba(0, 0, 0, 1)">); System.out.println(</span>"sets1中添加元素element001:"+jedis.sadd("sets1", "element001"<span style="color: rgba(0, 0, 0, 1)">)); System.out.println(</span>"sets1中添加元素element002:"+jedis.sadd("sets1", "element002"<span style="color: rgba(0, 0, 0, 1)">)); System.out.println(</span>"sets1中添加元素element003:"+jedis.sadd("sets1", "element003"<span style="color: rgba(0, 0, 0, 1)">)); System.out.println(</span>"sets1中添加元素element002:"+jedis.sadd("sets2", "element002"<span style="color: rgba(0, 0, 0, 1)">)); System.out.println(</span>"sets1中添加元素element003:"+jedis.sadd("sets2", "element003"<span style="color: rgba(0, 0, 0, 1)">)); System.out.println(</span>"sets1中添加元素element004:"+jedis.sadd("sets2", "element004"<span style="color: rgba(0, 0, 0, 1)">)); System.out.println(</span>"查看sets1集合中的所有元素:"+jedis.smembers("sets1"<span style="color: rgba(0, 0, 0, 1)">)); System.out.println(</span>"查看sets2集合中的所有元素:"+jedis.smembers("sets2"<span style="color: rgba(0, 0, 0, 1)">)); System.out.println(</span>"sets1和sets2交集:"+jedis.sinter("sets1", "sets2"<span style="color: rgba(0, 0, 0, 1)">)); System.out.println(</span>"sets1和sets2并集:"+jedis.sunion("sets1", "sets2"<span style="color: rgba(0, 0, 0, 1)">)); System.out.println(</span>"sets1和sets2差集:"+jedis.sdiff("sets1", "sets2"));<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">差集:set1中有,set2中没有的元素</span>
运行结果:
======================set========================== 清空库中所有数据:OK ============= 增 ============= 向 sets 集合中加入元素 element001:1 向 sets 集合中加入元素 element002:1 向 sets 集合中加入元素 element003:1 向 sets 集合中加入元素 element004:1 查看 sets 集合中的所有元素:[element001, element002, element003, element004]============= 删 =============
集合 sets 中删除元素 element003:1
查看 sets 集合中的所有元素:[element001, element002, element004]============= 改 =============
============= 查 =============
判断 element001 是否在集合 sets 中:true
循环查询获取 sets 中的每个元素:
element001
element002
element004============= 集合运算 =============
sets1 中添加元素 element001:1
sets1 中添加元素 element002:1
sets1 中添加元素 element003:1
sets1 中添加元素 element002:1
sets1 中添加元素 element003:1
sets1 中添加元素 element004:1
查看 sets1 集合中的所有元素:[element001, element002, element003]
查看 sets2 集合中的所有元素:[element002, element003, element004]
sets1 和 sets2 交集:[element002, element003]
sets1 和 sets2 并集:[element001, element002, element003, element004]
sets1 和 sets2 差集:[element001]
5)SortedSet 功能(有序集合)
private void SortedSetOperate() { System.out.println("======================zset=========================="); // 清空数据 System.out.println(jedis.flushDB());System.out.println(</span>"=============增============="<span style="color: rgba(0, 0, 0, 1)">); System.out.println(</span>"zset中添加元素element001:"+shardedJedis.zadd("zset", 7.0, "element001"<span style="color: rgba(0, 0, 0, 1)">)); System.out.println(</span>"zset中添加元素element002:"+shardedJedis.zadd("zset", 8.0, "element002"<span style="color: rgba(0, 0, 0, 1)">)); System.out.println(</span>"zset中添加元素element003:"+shardedJedis.zadd("zset", 2.0, "element003"<span style="color: rgba(0, 0, 0, 1)">)); System.out.println(</span>"zset中添加元素element004:"+shardedJedis.zadd("zset", 3.0, "element004"<span style="color: rgba(0, 0, 0, 1)">)); System.out.println(</span>"zset集合中的所有元素:"+shardedJedis.zrange("zset", 0, -1));<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">按照权重值排序</span>
System.out.println();
System.out.println(</span>"=============删============="<span style="color: rgba(0, 0, 0, 1)">); System.out.println(</span>"zset中删除元素element002:"+shardedJedis.zrem("zset", "element002"<span style="color: rgba(0, 0, 0, 1)">)); System.out.println(</span>"zset集合中的所有元素:"+shardedJedis.zrange("zset", 0, -1<span style="color: rgba(0, 0, 0, 1)">)); System.out.println(); System.out.println(</span>"=============改============="<span style="color: rgba(0, 0, 0, 1)">); System.out.println(); System.out.println(</span>"=============查============="<span style="color: rgba(0, 0, 0, 1)">); System.out.println(</span>"统计zset集合中的元素中个数:"+shardedJedis.zcard("zset"<span style="color: rgba(0, 0, 0, 1)">)); System.out.println(</span>"统计zset集合中权重某个范围内(1.0——5.0),元素的个数:"+shardedJedis.zcount("zset", 1.0, 5.0<span style="color: rgba(0, 0, 0, 1)">)); System.out.println(</span>"查看zset集合中element004的权重:"+shardedJedis.zscore("zset", "element004"<span style="color: rgba(0, 0, 0, 1)">)); System.out.println(</span>"查看下标1到2范围内的元素值:"+shardedJedis.zrange("zset", 1, 2<span style="color: rgba(0, 0, 0, 1)">)); }</span></pre>
运行结果:
======================zset========================== OK ============= 增 ============= zset 中添加元素 element001:1 zset 中添加元素 element002:1 zset 中添加元素 element003:1 zset 中添加元素 element004:1 zset 集合中的所有元素:[element003, element004, element001, element002]============= 删 =============
zset 中删除元素 element002:1
zset 集合中的所有元素:[element003, element004, element001]============= 改 =============
============= 查 =============
统计 zset 集合中的元素中个数:3
统计 zset 集合中权重某个范围内(1.0——5.0),元素的个数:2
查看 zset 集合中 element004 的权重:3.0
查看下标 1 到 2 范围内的元素值:[element004, element001]
6)Hash 功能
private void HashOperate() { System.out.println("======================hash=========================="); //清空数据 System.out.println(jedis.flushDB());System.out.println(</span>"=============增============="<span style="color: rgba(0, 0, 0, 1)">); System.out.println(</span>"hashs中添加key001和value001键值对:"+shardedJedis.hset("hashs", "key001", "value001"<span style="color: rgba(0, 0, 0, 1)">)); System.out.println(</span>"hashs中添加key002和value002键值对:"+shardedJedis.hset("hashs", "key002", "value002"<span style="color: rgba(0, 0, 0, 1)">)); System.out.println(</span>"hashs中添加key003和value003键值对:"+shardedJedis.hset("hashs", "key003", "value003"<span style="color: rgba(0, 0, 0, 1)">)); System.out.println(</span>"新增key004和4的整型键值对:"+shardedJedis.hincrBy("hashs", "key004", 4l<span style="color: rgba(0, 0, 0, 1)">)); System.out.println(</span>"hashs中的所有值:"+shardedJedis.hvals("hashs"<span style="color: rgba(0, 0, 0, 1)">)); System.out.println(); System.out.println(</span>"=============删============="<span style="color: rgba(0, 0, 0, 1)">); System.out.println(</span>"hashs中删除key002键值对:"+shardedJedis.hdel("hashs", "key002"<span style="color: rgba(0, 0, 0, 1)">)); System.out.println(</span>"hashs中的所有值:"+shardedJedis.hvals("hashs"<span style="color: rgba(0, 0, 0, 1)">)); System.out.println(); System.out.println(</span>"=============改============="<span style="color: rgba(0, 0, 0, 1)">); System.out.println(</span>"key004整型键值的值增加100:"+shardedJedis.hincrBy("hashs", "key004", 100l<span style="color: rgba(0, 0, 0, 1)">)); System.out.println(</span>"hashs中的所有值:"+shardedJedis.hvals("hashs"<span style="color: rgba(0, 0, 0, 1)">)); System.out.println(); System.out.println(</span>"=============查============="<span style="color: rgba(0, 0, 0, 1)">); System.out.println(</span>"判断key003是否存在:"+shardedJedis.hexists("hashs", "key003"<span style="color: rgba(0, 0, 0, 1)">)); System.out.println(</span>"获取key004对应的值:"+shardedJedis.hget("hashs", "key004"<span style="color: rgba(0, 0, 0, 1)">)); System.out.println(</span>"批量获取key001和key003对应的值:"+shardedJedis.hmget("hashs", "key001", "key003"<span style="color: rgba(0, 0, 0, 1)">)); System.out.println(</span>"获取hashs中所有的key:"+shardedJedis.hkeys("hashs"<span style="color: rgba(0, 0, 0, 1)">)); System.out.println(</span>"获取hashs中所有的value:"+shardedJedis.hvals("hashs"<span style="color: rgba(0, 0, 0, 1)">)); System.out.println(); }</span></pre>
运行结果:
======================hash========================== OK ============= 增 ============= hashs 中添加 key001 和 value001 键值对:1 hashs 中添加 key002 和 value002 键值对:1 hashs 中添加 key003 和 value003 键值对:1 新增 key004 和 4 的整型键值对:4 hashs 中的所有值:[value001, value002, value003, 4]============= 删 =============
hashs 中删除 key002 键值对:1
hashs 中的所有值:[value001, value003, 4]============= 改 =============
key004 整型键值的值增加 100:104
hashs 中的所有值:[value001, value003, 104]============= 查 =============
判断 key003 是否存在:true
获取 key004 对应的值:104
批量获取 key001 和 key003 对应的值:[value001, value003]
获取 hashs 中所有的 key:[key004, key003, key001]
获取 hashs 中所有的 value:[value001, value003, 104]
三、常用命令
1)连接操作命令
quit:关闭连接(connection)
auth:简单密码认证
help cmd: 查看 cmd 帮助,例如:help quit
2)持久化
save:将数据同步保存到磁盘
bgsave:将数据异步保存到磁盘
lastsave:返回上次成功将数据保存到磁盘的 Unix 时戳
shundown:将数据同步保存到磁盘,然后关闭服务
3)远程服务控制
info:提供服务器的信息和统计
monitor:实时转储收到的请求
slaveof:改变复制策略设置
config:在运行时配置 Redis 服务器
4)对 value 操作的命令
exists(key):确认一个 key 是否存在
del(key):删除一个 key
type(key):返回值的类型
keys(pattern):返回满足给定 pattern 的所有 key
randomkey:随机返回 key 空间的一个
keyrename(oldname, newname):重命名 key
dbsize:返回当前数据库中 key 的数目
expire:设定一个 key 的活动时间(s)
ttl:获得一个 key 的活动时间
select(index):按索引查询
move(key, dbindex):移动当前数据库中的 key 到 dbindex 数据库
flushdb:删除当前选择数据库中的所有 key
flushall:删除所有数据库中的所有 key
5)String
set(key, value):给数据库中名称为 key 的 string 赋予值 value
get(key):返回数据库中名称为 key 的 string 的 value
getset(key, value):给名称为 key 的 string 赋予上一次的 value
mget(key1, key2,…, key N):返回库中多个 string 的 value
setnx(key, value):添加 string,名称为 key,值为 value
setex(key, time, value):向库中添加 string,设定过期时间 time
mset(key N, value N):批量设置多个 string 的值
msetnx(key N, value N):如果所有名称为 key i 的 string 都不存在
incr(key):名称为 key 的 string 增 1 操作
incrby(key, integer):名称为 key 的 string 增加 integer
decr(key):名称为 key 的 string 减 1 操作
decrby(key, integer):名称为 key 的 string 减少 integer
append(key, value):名称为 key 的 string 的值附加 value
substr(key, start, end):返回名称为 key 的 string 的 value 的子串
6)List
rpush(key, value):在名称为 key 的 list 尾添加一个值为 value 的元素
lpush(key, value):在名称为 key 的 list 头添加一个值为 value 的 元素
llen(key):返回名称为 key 的 list 的长度
lrange(key, start, end):返回名称为 key 的 list 中 start 至 end 之间的元素
ltrim(key, start, end):截取名称为 key 的 list
lindex(key, index):返回名称为 key 的 list 中 index 位置的元素
lset(key, index, value):给名称为 key 的 list 中 index 位置的元素赋值
lrem(key, count, value):删除 count 个 key 的 list 中值为 value 的元素
lpop(key):返回并删除名称为 key 的 list 中的首元素
rpop(key):返回并删除名称为 key 的 list 中的尾元素
blpop(key1, key2,… key N, timeout):lpop 命令的 block 版本。
brpop(key1, key2,… key N, timeout):rpop 的 block 版本。
rpoplpush(srckey, dstkey):返回并删除名称为 srckey 的 list 的尾元素,
并将该元素添加到名称为 dstkey 的 list 的头部
7)Set
sadd(key, member):向名称为 key 的 set 中添加元素 member
srem(key, member) :删除名称为 key 的 set 中的元素 member
spop(key) :随机返回并删除名称为 key 的 set 中一个元素
smove(srckey, dstkey, member) :移到集合元素
scard(key) :返回名称为 key 的 set 的基数
sismember(key, member) :member 是否是名称为 key 的 set 的元素
sinter(key1, key2,…key N) :求交集
sinterstore(dstkey, (keys)) :求交集并将交集保存到 dstkey 的集合
sunion(key1, (keys)) :求并集
sunionstore(dstkey, (keys)) :求并集并将并集保存到 dstkey 的集合
sdiff(key1, (keys)) :求差集
sdiffstore(dstkey, (keys)) :求差集并将差集保存到 dstkey 的集合
smembers(key) :返回名称为 key 的 set 的所有元素
srandmember(key) :随机返回名称为 key 的 set 的一个元素
8)Hash
hset(key, field, value):向名称为 key 的 hash 中添加元素 field
hget(key, field):返回名称为 key 的 hash 中 field 对应的 value
hmget(key, (fields)):返回名称为 key 的 hash 中 field i 对应的 value
hmset(key, (fields)):向名称为 key 的 hash 中添加元素 field
hincrby(key, field, integer):将名称为 key 的 hash 中 field 的 value 增加 integer
hexists(key, field):名称为 key 的 hash 中是否存在键为 field 的域
hdel(key, field):删除名称为 key 的 hash 中键为 field 的域
hlen(key):返回名称为 key 的 hash 中元素个数
hkeys(key):返回名称为 key 的 hash 中所有键
hvals(key):返回名称为 key 的 hash 中所有键对应的 value
hgetall(key):返回名称为 key 的 hash 中所有的键(field)及其对应的 value