关注分享主机优惠活动
国内外VPS云服务器

第二,Rdeis Jedis

摘要:一八数据类型、二、三、四集群和一概述是官方推荐的连接开发工具。使您能够操作中间件。

一. Redis II的八种数据类型。Rdeis Jedis三世。雷迪斯生菜四。Redis集群一、Jedis概述Jedis是Redis官方推荐的Java连接开发工具。启用Java运行Redis中间件。

二、Jedis例1。Ping公共类测试ping { public static void main(string[]args){//创建连接jedis jedis = new jedis ("127.0.0.1 ",6379);System.out.printf("测试连接:%s ",jedis.ping())。println();//关闭连接jedis . Close();}} 2.password公共类测试密码{ public static void main(string[]args){//创建连接jedis jedis = new jedis ("127.0.0.1 ",6379);//未设置密码,跳过//system . out . printf(" Verify Password:% s ",jedis.auth ("123456 "))。println();//connect jedis . connect();//Disconnect jedis . Disconnect();//清除所有keyjedis。flush alls();//关闭连接jedis . Close();}} 3.key公共类测试key { public static void main(string[]args){//创建连接jedis jedis = new jedis ("127.0.0.1 ",6379);system . out . println(" empty data:"+jedis . flush db());System.out.println("判断用户名键是否存在:"+jedis . exists(" username "));System.out.println("新添加的键值对:"+jedis.set("username "," QS "));System.out.println("新增的键值对:"+jedis.set("password "," 123 "));System.out.print("系统中所有键如下:");set keys = jedis . keys(" * ");system . out . println(keys);System.out.println("删除密钥密码:"+jedis.del("密码"));System.out.println("判断密钥密码是否存在:"+jedis . exists(" password "));System.out.println("查看key username保存的值的类型:"+jedis . type(" username "));System.out.println("随机返回密钥空间key中的一个密钥:"+jedis . random key());system . out . println(" rename key:"+jedis . rename(" username "," name "));System.out.println("拿出修改过的名字:"+jedis . get(" name "));System.out.println("按索引查询:"+jedis . select(0));System.out.println("删除当前选中的数据库键中的所有键:"+jedis . flush db());System.out.println("返回当前数据库中的键数:"+jedis . dbsize());System.out.println("删除所有数据库中的所有键key:"+jedis . flush all());//关闭连接jedis . Close();}} 4.string公共类测试string { public static void main(string[]args){//创建连接jedis jedis = new jedis ("127.0.0.1 ",6379);jedis . flush db();系统。out.println ("= = = = = = = =添加数据= = = = = = ");system . out . println(jedis . set(" key 1 "," value 1 "));system . out . println(jedis . set(" key 2 "," value 2 "));system . out . println(jedis . set(" key 3 "," value 3 "));System.out.println("修改key 1:"+jedis.set ("key 1 "," value 1 changed "));System.out.println("获取key1的值:"+jedis . get(" key 1 "));system . out . println(" delete key 2:"+jedis . del(" key 2 "));system . out . println(" get key 2:"+jedis . get(" key 2 "));System.out.println("添加key3后的值:"+jedis.append("key3 "," End "));System.out.println("获取key3的值:"+jedis . Get(" key 3 "));System.out.println("添加多个键值对:"+jedis.mset ("key01 "," value01 "," key02 "," value02 "," key03 "," value 03 "));System.out.println("获取多个键值对:"+jedis.mget ("key01 "," key02 "," key 03 "));System.out.println("获取多个键值对:"+jedis.mget ("key01 "、" key02 "、" key03 "、" key 04 "));System.out.println("删除多个键值对:"+jedis.del("key01 "," key 02 "));System.out.println("获取多个键值对:"+jedis.mget ("key01 "," key02 "," key 03 "));jedis . flush db();系统。out . println(" = = = = = = = = = = =添加一个键-值对以防止覆盖原始值= = = = = = = = ");system . out . println(jedis . setnx(" key 1 "," value 1 "));system . out . println(jedis . setnx(" key 2 "," value 2 "));system . out . println(jedis . setnx(" key 2 "," value 2-new "));system . out . println(jedis . get(" key 1 "));system . out . println(jedis . get(" key 2 "));系统。out.println ("= = = = = = = =添加一个键值对并设置有效时间= = = = = = = ");system . out . println(jedis . setex(" key 3 ",2," value 3 "));system . out . println(jedis . get(" key 3 "));试试{ TimeUnit。seconds . sleep③;} catch(interrupted exception e){ e . printstacktrace();} system . out . println(jedis . get(" key 3 "));系统。out.println ("= = = = = = = =获取原始值并更新为新值= = = = = = ");system . out . println(jedis . getset(" key 2 "," key 2 getset "));system . out . println(jedis . get(" key 2 "));System.out.println("获取key2的值的字符串:"+jedis.getrange("key2 ",2,4));//关闭连接jedis . Close();}} 5.List公共类测试列表{ public static void main(string[]args){//创建连接jedis jedis = new jedis ("127.0.0.1 ",6379);jedis . flush db();系统。out.println ("= = = = = = = = =添加列表= = = = = = ");jedis.lpush("collections "、" ArrayList "、" Vector "、" Stack "、" HashMap "、" WeakHashMap "、" linked HashMap ");jedis.lpush("collections "," HashSet ");jedis.lpush("collections "," TreeSet ");jedis.lpush("collections "," TreeMap ");// -1表示倒数第二个元素,-2表示倒数第二个元素,-1结束表示查询所有系统。out.println("集合的内容:"+jedis.l range ("collections ",0,-1));系统。out.println("集合区间0-3的元素:"+jedis.lrange("集合",0,3));system . out . println(" = = = = = = = = = = = = = = = = = = = = = = = = = = ");//删除列表中指定的值。第二个参数是要删除的数(如果有重复的话),先删除增加的值,类似于System.out.println("删除指定元素的个数:"+jedis。lrem ("collections ",2," hashmap "));系统。out.println("集合的内容:"+jedis.l range ("collections ",0,-1));System.out.println("删除下标0-3区间外的元素:"+jedis.ltrim("collections ",0,3));系统。out.println("集合的内容:"+jedis.l range ("collections ",0,-1));系统。out . println(" Collections list off the stack(左端):"+jedis . lpop(" Collections "));系统。out.println("集合的内容:"+jedis.l range ("collections ",0,-1));系统。out . println(" collections add elements,从列表右端开始,对应lpush:"+jedis . rpush(" collections "," enum map "));系统。out.println("集合的内容:"+jedis.l range ("collections ",0,-1));系统。out . println(" Collections list off the stack(右端):"+jedis . rpop(" Collections "));系统。out.println("集合的内容:"+jedis.l range ("collections ",0,-1));System.out.println("修改集合指定的下标1的内容:"+jedis。lset ("Collections ",1," Linked ArrayList ");系统。out.println("集合的内容:"+jedis.l range ("collections ",0,-1));system . out . println(" = = = = = = = = = = = = = = = = = = = = = = = = = = ");系统。out.println("集合长度:"+jedis.llen("集合"));System.out.println("获取集合下标为2的元素:"+jedis.lindex("collections ",2));system . out . println(" = = = = = = = = = = = = = = = = = = = = = = = = = = ");jedis.lpush("sortedList "," 3 "," 6 "," 2 "," 0 "," 7 "," 4 ");System.out.println("排序前排序列表:"+jedis.l range("排序列表",0,-1));system . out . println(jedis . sort(" sorted list "));system . out . println(" sorted list:"+jedis . l range(" sorted list ",0,-1));//关闭连接jedis . Close();}} 6.set public class testset { public static void main(string[]args){//Create connection jedis jedis = new jedis(" 127 . 0 . 0 . 1 ",6379);jedis . flush db();系统。out . println(" = = = = = = = = =向集合中添加一个元素(不重复)= = = = = = = = = ";system . out . println(jedis . sadd(" eleSet "、" e1 "、" e2 "、" e4 "、" e3 "、" e0 "、" e8 "、" e7 "、" E5 "));system . out . println(jedis . sadd(" eleSet "," E6 "));system . out . println(jedis . sadd(" eleSet "," E6 "));系统。out . println(" eleSet "的所有元素都是:"+jedis . s members(" eleSet "));System.out.println(&

未经允许不得转载:主机频道 » 第二,Rdeis Jedis

评论 抢沙发

评论前必须登录!