Jar to be used:
Commons-pool2-2.3.jar
Jedis-2.7.0.jar
Configuration file for Jedispoolconfig redis.properties
redis.maxidle=30redis.minidle=10redis.maxtotal=100redis.url=Localhostredis.port =6379
Connection pooling tool classes for Redis database connections jedispoolutils
Importjava.io.IOException;ImportJava.io.InputStream;Importjava.util.Properties;ImportRedis.clients.jedis.Jedis;ImportRedis.clients.jedis.JedisPool;ImportRedis.clients.jedis.JedisPoolConfig; Public classJedispoolutils {Private StaticJedispool pool =NULL; Static{ //Load configuration fileInputStream in = Jedispoolutils.class. getClassLoader (). getResourceAsStream ("Redis.properties"); Properties Pro=NewProperties (); Try{pro.load (in); } Catch(IOException e) {e.printstacktrace (); } //Get Pool ObjectJedispoolconfig Poolconfig =NewJedispoolconfig (); Poolconfig.setmaxidle (Integer.parseint (Pro.get ("Redis.maxidle"). ToString ()));//Maximum Idle CountPoolconfig.setminidle (Integer.parseint (Pro.get ("Redis.minidle"). ToString ()));//minimum number of idlePoolconfig.setmaxtotal (Integer.parseint (Pro.get ("Redis.maxtotal"). ToString ()));//Maximum number of connectionsPool =NewJedispool (Poolconfig,pro.getproperty ("Redis.url"), Integer.parseint (Pro.get ("Redis.port")). toString ())); } //ways to get Jedis resources Public StaticJedis Getjedis () {returnPool.getresource (); } Public Static voidMain (string[] args) {Jedis Jedis=Getjedis (); System.out.println (Jedis.get ("XXX")); }}
Redis uses:
//queries from the cache first categorylist if there is direct use not being stored in the cache from the database query//1. Get the Jedis object to connect to the Redis databaseJedis Jedis =Jedispoolutils.getjedis (); String Categorylistjson= Jedis.get ("Categorylistjson");//2. Determine if the Categorylistjson is emptyif(Categorylistjson = =NULL) {System.out.println ("Cache does not have a data query database"); //preparing categorical data for querying from the databaseList<category> categorylist =service.findallcategory (); Gson Gson=NewGson (); Categorylistjson=Gson.tojson (categorylist); //put the queried data into the Redis databaseJedis.set ("Categorylistjson", Categorylistjson); }response.setcontenttype ("Text/html;charset=utf-8"); Response.getwriter (). write (Categorylistjson);
Using the Redis cache feature (Windows Redis)