您好,登錄后才能下訂單哦!
本文實例講述了Java連接redis及基本操作。分享給大家供大家參考,具體如下:
點擊此處:本站下載安裝。
解壓安裝
啟動redis:使用cd命令切換目錄到 D:\redis運行redis-server.exe redis.windows.conf
默認端口為6379
訪問:切換到redis目錄下運行 redis-cli.exe -h 127.0.0.1 -p 6379
。
pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>testRedis</groupId> <artifactId>testRedis</artifactId> <version>0.0.1-SNAPSHOT</version> <build/> <dependencies> <!-- Redis NoSQL 操作依賴 --> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.1.0</version> </dependency> </dependencies> </project>
測試代碼:
package com.crisy.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; public class RedisClient { /** * 一般項目基本都使用非切片池; * 切片池主要用于分布式項目,可以設置主從Redis庫 */ private Jedis jedis;//非切片客戶端連接 private JedisPool jedisPool;//非切片連接池 private ShardedJedis shardedJedis;//切片客戶端連接 private ShardedJedisPool shardedJedisPool;//切片連接池 public RedisClient(){ initialPool(); //初始化非切片連接池 initialShardedPool(); //初始化切片連接池 shardedJedis = shardedJedisPool.getResource(); jedis = jedisPool.getResource(); } private void initialPool() { JedisPoolConfig config = new JedisPoolConfig(); config.setMaxActive(20); config.setMaxIdle(5); config.setMaxWait(100001); config.setTestOnBorrow(false); jedisPool = new JedisPool(config,"127.0.0.1",6379); } private void initialShardedPool() { JedisPoolConfig config = new JedisPoolConfig(); config.setMaxActive(20); config.setMaxIdle(5); config.setMaxWait(100001); config.setTestOnBorrow(false); //slave連接 List<JedisShardInfo> shardInfos = new ArrayList<JedisShardInfo>(); shardInfos.add(new JedisShardInfo("127.0.0.1",6379,"master")); //構造池 shardedJedisPool = new ShardedJedisPool(config, shardInfos); } public void show(){ System.out.println("----對Value操作的命令----"); valueOperate(); System.out.println("----對String操作的命令----"); stringOperator(); System.out.println("----對List操作的命令----"); listOperator(); System.out.println("----對Set操作的命令----"); setOperator(); System.out.println("----對hash操作的命令----"); hashOperate(); } /** * 對Value操作的命令 * @throws InterruptedException */ private void valueOperate(){ System.out.println("清空當前選擇庫中所有key:"+jedis.flushDB()); System.out.println("清空所有庫中所有key:"+jedis.flushAll()); System.out.println("判斷myKey鍵是否存在:"+shardedJedis.exists("myKey")); System.out.println("新增myKey:"+shardedJedis.set("myKey","myValue")); System.out.println("判斷myKey鍵是否存在:"+shardedJedis.exists("myKey")); System.out.println("查看myKey所儲存的值的類型:"+jedis.type("myKey")); System.out.println("重命名myKey-myKey_1:"+jedis.rename("myKey", "myKey_1")); System.out.println("判斷myKey鍵是否存在:"+shardedJedis.exists("myKey")); System.out.println("判斷myKey_1鍵是否存在:"+shardedJedis.exists("myKey_1")); System.out.println("新增myKey_2:"+shardedJedis.set("myKey_2","myValue_2")); System.out.println("查看索引為0的Key:"+jedis.select(0)); System.out.println("當前數據庫中key的數目:"+jedis.dbSize()); System.out.println("系統中所有鍵如下:"); //返回滿足給定pattern的所有key Set<String> keys = jedis.keys("*"); Iterator<String> it=keys.iterator() ; while(it.hasNext()){ String key = it.next(); System.out.println(key); } //刪除時,如果不存在忽略此命令 System.out.println("系統中刪除myKey_3: "+jedis.del("myKey_3")); System.out.println("系統中刪除myKey_2: "+jedis.del("myKey_2")); System.out.println("判斷myKey_2是否存在:"+shardedJedis.exists("myKey_2")); System.out.println("設置 myKey_1的過期時間為5秒:"+jedis.expire("myKey_1", 5)); try { Thread.sleep(2000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } //沒有設置過期時間或者不存在的都返回-1 System.out.println("獲得myKey_1的剩余活動時間:"+jedis.ttl("myKey_1")); System.out.println("取消設置myKey_1的活動時間:"+jedis.persist("myKey_1")); System.out.println("查看myKey_1的剩余活動時間:"+jedis.ttl("myKey_1")); } /** * 對String操作的命令 */ private void stringOperator(){ System.out.println("清空當前選擇庫中所有key:"+jedis.flushDB()); System.out.println("給數據庫中名稱為key01的string賦予值value:"+jedis.set("key01", "value01")); System.out.println("給數據庫中名稱為key02的string賦予值value:"+jedis.set("key02", "value02")); System.out.println("給數據庫中名稱為key03的string賦予值value:"+jedis.set("key03", "value03")); System.out.println("返回數據庫中名稱為key01的string的value:"+jedis.get("key01")); System.out.println("返回庫中多個string的value:"+jedis.mget("key01","key02","key03")); System.out.println("添加string,名稱為key,值為value:"+jedis.setnx("key04","value04")); System.out.println("向庫中添加string,設定過期時間time:"+jedis.setex("key05",5,"value05")); System.out.println("批量設置多個string的值:"+jedis.mset("key06_1","value06_1","key06_2","value06_2")); System.out.println("給數據庫中名稱為key07的string賦予值value:"+jedis.set("key07", "1")); System.out.println("名稱為key07的string增1操作:"+jedis.incr("key07")); //ERR value is not an integer or out of range //System.out.println("名稱為key01的string增1操作:"+jedis.incr("key01")); System.out.println("名稱為key07的string增加integer:"+jedis.incrBy("key07", 5)); System.out.println("名稱為key07的string減1操作:"+jedis.decr("key07")); System.out.println("名稱為key07的string減少integer:"+jedis.decrBy("key07", 3)); System.out.println("名稱為key的string的值附加value:"+jedis.append("key01","Hello World")); System.out.println("返回名稱為key的string的value的子串:"+jedis.substr("key01",0,5)); } /** * 對List操作的命令 */ private void listOperator(){ System.out.println("清空當前選擇庫中所有key:"+jedis.flushDB()); //準備兩個list shardedJedis.lpush("mylist", "Java"); shardedJedis.lpush("mylist", "C"); shardedJedis.lpush("mylist", "C++"); shardedJedis.lpush("mylist", "Java"); System.out.println("在名稱為mylist的list尾添加一個值為value的元素:"+jedis.rpush("mylist","phython")); System.out.println("在名稱為mylist的list頭添加一個值為value的 元素:"+jedis.rpush("mylist","PHP")); System.out.println("返回名稱為mylist的list的長度:"+jedis.llen("mylist")); System.out.println("返回名稱為mylist中start至end之間的元素:"+jedis.lrange("mylist",0,3)); System.out.println("截取名稱為mylist的list:"+jedis.ltrim("mylist",0,3)); System.out.println("返回名稱為mylist的list中index位置的元素:"+jedis.lindex("mylist",0)); System.out.println("給名稱為mylist的list中index位置的元素賦值:"+jedis.lset("mylist",0,"hahahahah我改啦")); System.out.println("刪除count個mylist的key中值為value的元素:"+jedis.lrem("mylist",1,"Java")); System.out.println("返回并刪除名稱為mylist的list中的首元素:"+jedis.lpop("mylist")); System.out.println("返回并刪除名稱為mylist的list中的尾元素:"+jedis.rpop("mylist")); } /** * 對Set操作的命令 */ private void setOperator(){ System.out.println("清空當前選擇庫中所有key:"+jedis.flushDB()); //準備兩個set jedis.sadd("myset","hahaha"); jedis.sadd("myset","hahaha"); jedis.sadd("myset","lalala"); jedis.sadd("myset","kakaka"); jedis.sadd("myset2","hahaha"); jedis.sadd("myset2","lalala"); jedis.sadd("myset2","kakaka"); System.out.println("向名稱為myset的set中添加元素member:"+jedis.sadd("myset","wawawa")); System.out.println("返回名稱為myset的set的所有元素:"+jedis.smembers("myset")); System.out.println("刪除名稱為myset的set中的元素member:"+jedis.srem("myset","wawawa")); System.out.println("返回名稱為myset的set的所有元素:"+jedis.smembers("myset")); System.out.println("隨機返回并刪除名稱為myset的set中一個元素:"+jedis.spop("myset")); System.out.println("移到集合元素:"+jedis.smove("myset","myset2","hahaha")); System.out.println("返回名稱為myset的set的所有元素:"+jedis.smembers("myset")); System.out.println("返回名稱為myset2的set的所有元素:"+jedis.smembers("myset2")); System.out.println("返回名稱為mykey的set的基數:"+jedis.scard("myset")); System.out.println("member是否是名稱為mykey的set的元素:"+jedis.sismember("myset","hahaha")); System.out.println("求交集:"+jedis.sinter("myset","myset2")); System.out.println("求交集并將交集保存到dstkey的集合:"+jedis.sinterstore("dstkey","myset2","myset")); System.out.println("返回名稱為dstkey的set的所有元素:"+jedis.smembers("dstkey")); System.out.println("求并集:"+jedis.sunion("myset","myset2")); System.out.println("求并集并將并集保存到dstkey的集合:"+jedis.sinterstore("dstkey","myset2","myset")); System.out.println("返回名稱為dstkey的set的所有元素:"+jedis.smembers("dstkey")); System.out.println("求差集:"+jedis.sdiff("myset2","myset")); System.out.println("求差集并將差集保存到dstkey的集合:"+jedis.sdiffstore("dstkey","myset2","myset")); System.out.println("返回名稱為dstkey的set的所有元素:"+jedis.smembers("dstkey")); System.out.println("隨機返回名稱為dstkey的set的一個元素:"+jedis.srandmember("dstkey")); } /** * hash */ private void hashOperate(){ System.out.println(jedis.flushDB()); System.out.println("向名稱為key的hash中添加元素field1:"+shardedJedis.hset("key", "field1", "value1")); System.out.println("向名稱為key的hash中添加元素field2:"+shardedJedis.hset("key", "field2", "value2")); System.out.println("向名稱為key的hash中添加元素field3:"+shardedJedis.hset("key", "field3", "value3")); System.out.println("返回名稱為key的hash中field1對應的value:"+shardedJedis.hget("key", "field1")); System.out.println("返回名稱為key的hash中field1對應的value:"+shardedJedis.hmget("key", "field1","field2","field3")); System.out.println("刪除名稱為key的hash中鍵為field1的域:"+shardedJedis.hdel("key", "field1")); System.out.println("返回名稱為key的hash中所有鍵對應的value:"+shardedJedis.hvals("key")); System.out.println("返回名稱為key的hash中元素個數:"+shardedJedis.hlen("key")); System.out.println("返回名稱為key的hash中所有鍵:"+shardedJedis.hkeys("key")); System.out.println("返回名稱為key的hash中所有的鍵(field)及其對應的value:"+shardedJedis.hgetAll("key")); System.out.println("名稱為key的hash中是否存在鍵為field1的域:"+shardedJedis.hexists("key","field1")); } }
更多關于java相關內容感興趣的讀者可查看本站專題:《Java+MySQL數據庫程序設計總結》、《Java操作Excel技巧總結》、《Java數據結構與算法教程》、《Java文件與目錄操作技巧匯總》及《Java操作DOM節點技巧總結》
希望本文所述對大家java程序設計有所幫助。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。