Redis Cache System-java-jedis operation Redis, basic operations and implementation of object saving

Source: Internet
Author: User

Source code Download: http://download.csdn.net/detail/jiangtao_st/7623113

1. Maven Configuration

<dependency>  <groupId>redis.clients</groupId>  <artifactid>jedis</artifactid >  <version>2.5.0</version></dependency><dependency>  <groupId> com.alibaba</groupid>  <artifactId>fastjson</artifactId>  <version>1.1.41</ Version></dependency></span>

2. Properties configuration file

Redis.pool.maxactive= 100

redis.pool.maxidle= 20

redis.pool.maxwait= 3000

redis.ip= localhost

Redis.port= 6379

3, the specific implementation of the client code

/** * * <p> * Redis Client Access * </p> * * @author Check Inn HK * @ creation time: July 11, 2014 * @version: V1.0 */public class Redisclien     t {public static jedispool Jedispool;    static {ResourceBundle ResourceBundle = Resourcebundle.getbundle ("Redis");    int maxactive = Integer.parseint (resourcebundle.getstring ("redis.pool.maxActive"));    int maxidle = Integer.parseint (resourcebundle.getstring ("Redis.pool.maxIdle"));        int maxwait = Integer.parseint (resourcebundle.getstring ("redis.pool.maxWait"));    String IP = resourcebundle.getstring ("Redis.ip");        int port = integer.parseint (resourcebundle.getstring ("Redis.port"));          Jedispoolconfig config = new Jedispoolconfig ();        Config.setmaxtotal (maxactive);        Config.setmaxidle (Maxidle);            Config.setmaxwaitmillis (maxwait);   Jedispool = new Jedispool (config, IP, port); /** * Set string contents to cache * @param key key * @param value value * @return * @throws Exception */public static B Oolean SET (String key,string value) throws exception{Jedis Jedis = null;      try {Jedis = Jedispool.getresource ();      Jedis.set (key, value);    return true;      } catch (Exception e) {e.printstacktrace ();    return false;    }finally{Jedispool.returnresource (Jedis); }}/** * Set object to cache * @param key * @param value * @return */public static Boolean set (String Key,object V    Alue) {Jedis Jedis = null;      try {String Objectjson = json.tojsonstring (value);      Jedis = Jedispool.getresource ();      Jedis.set (key, Objectjson);    return true;      } catch (Exception e) {e.printstacktrace ();    return false;    }finally{Jedispool.returnresource (Jedis); }}/** * Delete the cached object, according to key * @param key * @return */public static Boolean del (String key) {Jedis Jedis = Nu    ll      try {Jedis = Jedispool.getresource ();      Jedis.del (key);    return true;      } catch (Exception e) {e.printstacktrace ();   return false; }finally{Jedispool.returnresource (Jedis);    }}/** * Get content based on key * @param key * @return */public static Object get (String key) {Jedis Jedis = null;      try {Jedis = Jedispool.getresource ();      Object value = Jedis.get (key);    return value;      } catch (Exception e) {e.printstacktrace ();    return false;    }finally{Jedispool.returnresource (Jedis); }}/** * Gets the object based on key * @param key * @return */public static <T> T get (String key,class<t> clazz)    {Jedis Jedis = null;      try {Jedis = Jedispool.getresource ();      String value = Jedis.get (key);    return Json.parseobject (value, clazz);      } catch (Exception e) {e.printstacktrace ();    return null;    }finally{Jedispool.returnresource (Jedis); }  }}

4. Unit test, Save object, write Object

/** *  * <p> *  Test standalone REDIS client * </p> *  * @author Check Inn HK * @ created: July 11, 2014 * @version: V1.0 */public CLA SS SimpleClient {    @Test public  void Usercache () {            Userdo Zhuoxuan = new Userdo ();    Zhuoxuan.setuserid (113445);    Zhuoxuan.setsex (1);    Zhuoxuan.setuname ("Check Inn HK");    Zhuoxuan.setunick ("Zhuoxuan");    Zhuoxuan.setemail ("[email protected]");        Boolean reusltcache = Redisclient.set ("Zhuoxuan", Zhuoxuan);    if (reusltcache) {      System.out.println ("Save object to cache succeeded. ");    } else{      System.out.println ("Failed to save object to cache. ");    }  }      @Test public  void GetUserInfo () {        Userdo Zhuoxuan = Redisclient.get ("Zhuoxuan", userdo.class);    if (Zhuoxuan! = null) {      System.out.println ("Object fetched from cache," + zhuoxuan.getuname () + "@" + zhuoxuan.getemail ());    }      }    }

Redis Cache System-java-jedis operation Redis, basic operations and implementation of object saving

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.