您好,登錄后才能下訂單哦!
這篇文章給大家介紹Java中Redis如何使用,內容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。
Redis 支持 32 位和 64 位。這個需要根據你系統平臺的實際情況選擇,這里我們下載 Redis-x64-3.2.100.zip壓縮包,并解壓至磁盤指定文件夾
打開文件夾,內容如下:
雙擊redis-server啟動redis,這時Redis默認地址是127.0.0.1,端口號是6379
雙擊,配置解壓目錄,并進行解壓
雙擊 redisclient-win32.x86.2.0.exe,即可運行,運行如圖
在maven下添加以下內容,引入jedis
<dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.9.0</version> </dependency>
編寫對象序列化與反序列化工具類
package com.jedis.util; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; public class SerializeUtil { /* * 序列化 * */ public static byte[] serizlize(Object object){ ObjectOutputStream oos = null; ByteArrayOutputStream baos = null; try { baos = new ByteArrayOutputStream(); oos = new ObjectOutputStream(baos); oos.writeObject(object); byte[] bytes = baos.toByteArray(); return bytes; } catch (Exception e) { e.printStackTrace(); }finally { try { if(baos != null){ baos.close(); } if (oos != null) { oos.close(); } } catch (Exception e2) { e2.printStackTrace(); } } return null; } /* * 反序列化 * */ public static Object deserialize(byte[] bytes){ ByteArrayInputStream bais = null; ObjectInputStream ois = null; try{ bais = new ByteArrayInputStream(bytes); ois = new ObjectInputStream(bais); return ois.readObject(); }catch(Exception e){ e.printStackTrace(); }finally { try { if (ois != null) { ois.close(); } if(bais != null){ bais.close(); } } catch (Exception e2) { e2.printStackTrace(); } } return null; } }
編寫Jedis工具類
package com.jedis.util; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Component; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; import java.util.Map; public class JedisComponent { private JedisPool jedisPool; public JedisComponent(JedisPool jedisPool){ this.jedisPool = jedisPool; } public void setObject(String key,Object object){ Jedis jedis = null; try { jedis = jedisPool.getResource(); jedis.set(key.getBytes(), SerializeUtil.serizlize(object)); } finally { if (jedis != null) { jedis.close(); } } } public Boolean exists(String key){ Jedis jedis = null; try { jedis = jedisPool.getResource(); return jedis.exists(key); } finally { if (jedis != null) { jedis.close(); } } } public Object getObject(String key){ Jedis jedis = null; try { jedis = jedisPool.getResource(); byte[] bytes = jedis.get(key.getBytes()); return SerializeUtil.deserialize(bytes); } finally { if (jedis != null) { jedis.close(); } } } public void setMap(String key, Map map) { Jedis jedis = null; try { jedis = jedisPool.getResource(); jedis.hmset(key,map); } finally { if (jedis != null) { jedis.close(); } } } public Map getMap(String key) { Jedis jedis = null; try { jedis = jedisPool.getResource(); return jedis.hgetAll(key); } finally { if (jedis != null) { jedis.close(); } } } public void set(String key, String val) { Jedis jedis = null; try { jedis = jedisPool.getResource(); jedis.set(key, val); } finally { if (jedis != null) { jedis.close(); } } } public String get(String key) { Jedis jedis = null; try { jedis = jedisPool.getResource(); return jedis.get(key); } finally { if (jedis != null) { jedis.close(); } } } public void setKeyTime(String key,int time) { Jedis jedis = null; try { jedis = jedisPool.getResource(); jedis.expire(key,time); } finally { if (jedis != null) { jedis.close(); } } } public void delKey(String key) { Jedis jedis = null; try { jedis = jedisPool.getResource(); jedis.del(key); } finally { if (jedis != null) { jedis.close(); } } } public Long getKeyTime(String key) { Jedis jedis = null; try { jedis = jedisPool.getResource(); return jedis.ttl(key); } finally { if (jedis != null) { jedis.close(); } } } public Long removeKeyTime(String key){ Jedis jedis = null; try { jedis = jedisPool.getResource(); return jedis.persist(key); } finally { if (jedis != null) { jedis.close(); } } } }
編寫測試對象Student
package com.jedisTest; import java.io.Serializable; /** * Created by Administrator on 2019/7/16 0016. */ public class Student implements Serializable{ private static final long serialVersionUID = 8562001374896568949L; private String fid; private String name; private Integer age; public String getFid() { return fid; } public void setFid(String fid) { this.fid = fid; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } @Override public String toString() { return "Student{" + "fid='" + fid + '\'' + ", name='" + name + '\'' + ", age='" + age + '\'' + '}'; } }
最后編寫main方法測試
package com.jedisTest; import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPoolConfig; import java.util.HashMap; import java.util.Map; import java.util.UUID; /** * Created by Administrator on 2019/7/16 0016. */ public class JedisTest { //路由 private static String host="127.0.0.1"; //端口號 private static int port=6379; //超時 private static int timeout=10000; // private String password="supermap"; //連接池最大連接數(使用負值表示沒有限制) private static int maxActive=1024; //連接池中的最大空閑連接 private static int maxIdle=200; //連接池中最小空閑連接 private static int minIdle=0; //最大等待 private static long maxWaitMillis=10000; public static void main(String[] args){ JedisPool jedisPool = null; try{ JedisPoolConfig jedisPoolConfig = new JedisPoolConfig(); jedisPoolConfig.setMaxIdle(maxIdle); jedisPoolConfig.setMaxWaitMillis(maxWaitMillis); jedisPoolConfig.setMaxTotal(maxActive); jedisPoolConfig.setMinIdle(minIdle); jedisPool = new JedisPool(jedisPoolConfig,host,port,timeout,null); System.out.println("JedisPool資源池注入成功!"); System.out.println("redis地址:" + host + ":" + port); JedisComponent jedisComponent = new JedisComponent(jedisPool);//實例化JedisComponent工具類 //字符串的操作 String today = "2019-07-16"; jedisComponent.set("date", today);//將字符串today存入Redis String value = jedisComponent.get("date");//將key為date的value取出 System.out.println("今天的日期是:" + value);//打印value到控制臺 jedisComponent.setKeyTime("date",10);//給date設置過期時間,單位是秒 Boolean date = jedisComponent.exists("date");//判斷key為date的數據是否存在 jedisComponent.removeKeyTime("date");//刪除date的過期時間 jedisComponent.delKey("date");//刪除key為date的數據 //對象的操作 Student stuSave1 = new Student(); stuSave1.setFid(UUID.randomUUID().toString()); stuSave1.setName("小明"); stuSave1.setAge(10); jedisComponent.setObject("stu1",stuSave1);//將stu1序列化存入Redis Student stuGet1 = (Student) jedisComponent.getObject("stu1");//將stu1反序列化取出 System.out.println(stuGet1.toString());//打印stuGet1到控制臺 jedisComponent.setKeyTime("stu1",10);//給stu1設置過期時間,單位是秒 //Map集合操作 Map<String, String> map = new HashMap<>();//Redis只支持Map集合鍵值都為String類型的存取 map.put("水果","香蕉"); map.put("家具","沙發"); jedisComponent.setMap("map", map);//將map存入Redis Map<String, String> map1 = jedisComponent.getMap("map");//將key為map從Redis取出 if(map1 != null){ for (Map.Entry<String, String> entry : map1.entrySet()) { System.out.println("key = " + entry.getKey() + ", value = " + entry.getValue()); } } jedisComponent.delKey("map");//刪除key為map的數據 }catch (Exception e){ e.printStackTrace(); }finally { jedisPool.close();//最后關閉JedisPool資源池 } } }
控制臺輸出
關于Java中Redis如何使用就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。