Java Simple to use Redis

Source: Internet
Author: User
Tags object object

1. Configuration files

  <BeanID= "Jedispoolconfig"class= "Redis.clients.jedis.JedisPoolConfig">        < Propertyname= "Maxtotal"value= " the" />        < Propertyname= "Maxidle"value= "8" />        < Propertyname= "Maxwaitmillis"value= "+" />        < Propertyname= "Testonborrow"value= "true"/>        < Propertyname= "Testonreturn"value= "true"/>    </Bean>    <BeanID= "Jedispool"class= "Redis.clients.jedis.JedisPool">        <Constructor-argIndex= "0"ref= "Jedispoolconfig" />        <Constructor-argIndex= "1"value= "192.168.1.76"type= "Java.lang.String"/>        <Constructor-argIndex= "2"value= "6379"type= "int"/>    </Bean>

2. Complex objects, stored in Redis by serialization into the binary

@Repository ("Rediscacheimpl") Public classRediscacheimplImplementsIrediscache {Private Static FinalLogger Logger = Loggerfactory.getlogger (Rediscacheimpl.class); @Autowired (Required=false)    protectedJedispool Pool; @Override Public voidput (String key, Object value) {Jedis Jedis=NULL; Try{Jedis=Pool.getresource ();        Jedis.set (Key.getbytes (), serializeutil.serialize (value)); } Catch(Exception e) {logger.error ("Redis Error:", E); } finally {            if(Jedis! =NULL) {jedis.close (); }}} @SuppressWarnings ({"Unchecked"}) @Override Public<T>T get (String key) {Jedis Jedis=NULL; Try{Jedis=Pool.getresource (); byte[] Value =Jedis.get (Key.getbytes ()); if(Value = =NULL) {                return NULL; }            return(T) serializeutil.unserialize (value); } Catch(Exception e) {logger.error ("Redis Error:", E); return NULL; } finally {            if(Jedis! =NULL) {jedis.close (); }}} @SuppressWarnings ({"Unchecked"}) @Override Public Booleandel (String key) {Jedis Jedis=NULL; Try{Jedis=Pool.getresource (); returnJedis.del (Key.getbytes ()) > 0; } Catch(Exception e) {logger.error ("Redis Error:", E); return false; } finally {            if(Jedis! =NULL) {jedis.close (); }        }    }}

3. Serialization Classes

 Public classSerializeutil {Private Static FinalLogger Logger = Loggerfactory.getlogger (serializeutil.class);  Public Static byte[] Serialize (Object object) {ObjectOutputStream Oos=NULL; Bytearrayoutputstream BAOs=NULL; Try {            //Serialization ofBAOs =NewBytearrayoutputstream (); Oos=NewObjectOutputStream (BAOs);            Oos.writeobject (object); byte[] bytes =Baos.tobytearray (); returnbytes; } Catch(Exception e) {logger.error ("Serializer Error:", E); }        return NULL; }     Public StaticObject Unserialize (byte[] bytes) {Bytearrayinputstream Bais=NULL; Try {            //deserializationBais =Newbytearrayinputstream (bytes); ObjectInputStream Ois=NewObjectInputStream (Bais); returnOis.readobject (); } Catch(Exception e) {logger.error ("Serializer Error:", E); }        return NULL; }}

Java Simple to use 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.