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