Java中Redis如何使用
这篇文章给大家介绍Java中Redis如何使用,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。
1、安装
Redis 支持 32 位和 64 位。这个需要根据你系统平台的实际情况选择,这里我们下载 Redis-x64-3.2.100.zip压缩包,并解压至磁盘指定文件夹
2、启动
打开文件夹,内容如下:
双击redis-server启动redis,这时Redis默认地址是127.0.0.1,端口号是6379
双击,配置解压目录,并进行解压
双击 redisclient-win32.x86.2.0.exe,即可运行,运行如图
3、Java使用Redis
在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如何使用就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。