In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces how to use Redis in Java, the content is very detailed, interested friends can refer to, hope to be helpful to you.
1. Installation
Redis supports 32-bit and 64-bit. This needs to be chosen according to the actual situation of your system platform. Here we download the Redis-x64-3.2.100.zip package and extract it to the specified folder on disk.
2. Start
Open the folder with the following contents:
Double-click redis-server to start redis, and the default address of Redis is 127.0.0.1 and the port number is 6379.
Double-click, configure the extract directory, and extract it
Double-click redisclient-win32.x86.2.0.exe to run, as shown in the figure
3. Java uses Redis
Add the following under maven to introduce jedis
Redis.clients jedis 2.9.0
Write object serialization and anti-sequence chemical tools classes
Package com.jedis.util;import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;public class SerializeUtil {/ * * Serialization * * / 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 } / * * deserialization * * / 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;}}
Write Jedis utility classes
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 ();}
Write a test object 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 +'\'+'}';}}
Finally, the main method test is written.
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 {/ / routing private static String host= "127.0.0.1"; / / Port number private static int port=6379; / / timeout private static int timeout=10000; / / private String password= "supermap"; / / maximum number of connections in the connection pool (using negative values for no limit) private static int maxActive=1024; / / maximum free connection private static int maxIdle=200 in the connection pool / / minimum idle connection private static int minIdle=0; / / maximum waiting 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 resource pool injection succeeded!") ; System.out.println ("redis address:" + host + ":" + port); JedisComponent jedisComponent = new JedisComponent (jedisPool); / / instantiate the JedisComponent utility class / / string operation String today = "2019-07-16"; jedisComponent.set ("date", today); / / store the string today in Redis String value = jedisComponent.get ("date") / / take out the System.out.println if the key is date ("Today's date is:" + value); / / print the value to the console jedisComponent.setKeyTime ("date", 10); / / set the expiration time for the date in seconds Boolean date = jedisComponent.exists ("date"); / / determine whether there is jedisComponent.removeKeyTime ("date") in the data where key is date / delete the expiration time of date jedisComponent.delKey ("date"); / / delete data with key of date / / the operation of / object Student stuSave1 = new Student (); stuSave1.setFid (UUID.randomUUID (). ToString ()); stuSave1.setName ("Xiaoming"); stuSave1.setAge (10); jedisComponent.setObject ("stu1", stuSave1) / / storing stu1 serialization in Redis Student stuGet1 = (Student) jedisComponent.getObject ("stu1"); / / deserializing stu1 to take out System.out.println (stuGet1.toString ()); / / printing stuGet1 to console jedisComponent.setKeyTime ("stu1", 10); / / setting expiration time for stu1 in seconds / / Map collection operation Map map = new HashMap () / Redis only supports access to map.put ("fruit", "banana") with Map collection keys of type String; map.put ("furniture", "sofa"); jedisComponent.setMap ("map", map); / / storing map in Redis Map map1 = jedisComponent.getMap ("map") / / if (map1! = null) {for (Map.Entry entry: map1.entrySet ()) {System.out.println ("key =" + entry.getKey () + ", value =" + entry.getValue ());}} jedisComponent.delKey ("map") from Redis to map / / Delete data with map key} catch (Exception e) {e.printStackTrace ();} finally {jedisPool.close (); / / finally close the JedisPool resource pool}
Console output
On how to use Redis in Java to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.