Using the Redis cache feature (Windows Redis)

Source: Internet
Author: User
Tags connection pooling

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)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.