Java Spring and Redis operations package source code

Source: Internet
Author: User

Redis is an open source, advanced key-value Storage and a perfect solution for building high-performance, scalable Web applications.


The three main features that Redis inherits from its many competitions are:

The Redis database is completely in memory and uses disk for persistence only.

Redis has a rich set of data types compared to many key-value data stores.

Redis can replicate data to any number of slave servers.


The advantages of Redis are as follows:

Exceptionally fast: Redis is very fast and can perform about 110,000 episodes per second, about 81000 + records per second.

Support for rich data types: Redis support Most developers already know like lists, collections, ordered collections, hash data types. This makes it very easy to solve a wide variety of problems because we know which issues are better than the data types that can be handled through it.

Operations are atomic: All Redis operations are atomic, which ensures that Redis servers accessed by two clients at the same time will get the updated values.

Multifunction utility: Redis is a versatile tool that can be used in multiple uses such as cache, message, queue (Redis native support publish/subscribe), any transient data, applications such as Web application sessions, Web page hits count etc.


Spring-data-redis Package Redisservice operation package source code as follows:

package com.yoodb.service;import java.util.arraylist;import java.util.hashset;import  java.util.list;import java.util.map;import java.util.set;import javax.annotation.resource; import org.apache.shiro.dao.dataaccessexception;import  org.springframework.data.redis.connection.datatype;import  org.springframework.data.redis.connection.redisconnection;import  org.springframework.data.redis.connection.redislistcommands.position;import  org.springframework.data.redis.connection.rediszsetcommands.tuple;import  org.springframework.data.redis.connection.sortparameters;import  org.springframework.data.redis.core.rediscallback;import  org.springframework.data.redis.core.redistemplate;import  org.springframework.data.redis.serializer.redisserializer;import org.springframework.stereotype.service;/ ** *  @author  yoodb * @   Motobumi  *  @url  www.yoodb.com * @ Note   Reprint Please specify WWW.YOODB.com *  @param  <K> *  @param  <v> */@Servicepublic  class  copyofredisservice<k, v> {@Resource (name =  "redistemplate") protected  redistemplate<k, v> redistemplate;/** *  Set Key */public boolean set ( Final string key, final string value)  {if  (redistemplate != null)  {redistemplate.execute (New rediscallback<boolean> ()  {public boolean doinredis ( Redisconnection connection) throws dataaccessexception {redisserializer<string>  Serializer = getredisserializer (); Byte[] keys = serializer.serialize (key); byte[]  values = serializer.serialize (value); Connection.set (keys, values); return true;}); Return false;} /** *  Get Object  */public string get (Final string key) based on key  {if  ( redistemplate != null)  {redistemplate.execute (new rediscallback<string> ()  {public String  doinredis (redisconnection connection) throws dataaccessexception {redisserializer< String> serializer = getredisserializer (); Byte[] keys = serializer.serialize ( key); Byte[] values = connection.get (keys);if  (values == null)  {return  null;} String value = serializer.deserialize (values); return value;});} Return null;} /** *  Delete  *  @param  key *  @return  */public long del based on key (final  string key)  {if  (redistemplate != null)  {redistemplate.execute (new  Rediscallback<long> ()  {public long doinredis (redisconnection connection) throws  Dataaccessexception {redisserializer<string> serializer = getredisserializer (); byte[ ] keys = Serializer.serialize (key); Return connection.del (keys);}}); Return null;} /** *  Execute  *  @param  key *  @param  value *  @return  */after a certain period of time Public boolean expire (Final string key, final long value)  {if  ( Redistemplate != null)  {redistemplate.execute (new rediscallback<boolean> ()  { Public boolean doinredis (redisconnection connection) throws dataaccessexception { Redisserializer<string> serializer = getredisserializer ();byte[] keys =  Serializer.serialize (key); Return connection.expire (Keys, value);}}); Return false;} /** *  expires at some point  *  @param  key *  @param  value *  @return  */ Public boolean expireat (Final string key, final long value)  {if  ( redistemplate != null)  {redistemplate.execute (NEW rediscallback<boolean> ()  {public boolean doinredis (RedisConnection connection ) throws dataaccessexception {redisserializer<string> serializer =  Getredisserializer (); Byte[] keys = serializer.serialize (key); Return connection.expireat ( Keys, value);}});} Return false;} /** *  query remaining time  *  @param  key *  @param  value *  @return  */ Public long ttl (Final string key, final long value)  {if  ( Redistemplate != null)  {redistemplate.execute (new rediscallback<long> ()  {public  long doinredis (redisconnection connection) throws dataaccessexception { Redisserializer<string> serializer = getredisserializer ();byte[] keys =  Serializer.serialize (key); Return connection.ttl (keys);}}); return 0l;} /** *  determine if key exists  * @param key *  @return  */public boolean exists (final string key)  {if   (Redistemplate != null)  {redistemplate.execute (new rediscallback<boolean> ()  {public boolean doinredis (redisconnection connection) throws dataaccessexception  {redisserializer<string> serializer = getredisserializer (); byte[] keys =  serializer.serialize (key); return connection.exists (keys);}}); Return false;} /** *  returns the type of value stored  key   *  @param  key *  @return  */public  Datatype type (Final string key)  {if  (redistemplate != null)  { Redistemplate.execute (New rediscallback<datatype> ()  {public datatype doinredis ( Redisconnection connection) throws dataaccessexception {redisserializer<string>  Serializer = getredisserializer (); byte[] kEys = serializer.serialize (key); Return connection.type (keys);}}); Return null;} /** *  the string value stored by  key , sets or clears the bit (bit)  *  @param  key *  @param on the specified offset  offset *  @param  value *  @return  */public boolean setbit (final  string key,final long offset,final boolean value)  {if  (redisTemplate  != null)  {redistemplate.execute (new rediscallback<boolean> ()  {public  Boolean doinredis (redisconnection connection) Throws dataaccessexception {redisserializer <string> serializer = getredisserializer ();byte[] keys =  Serializer.serialize (key); Connection.setbit (keys,offset,value); return true;}); Return false;} /** *  the string value stored by  key , gets the bit (bit) on the specified offset  *  @param  key *  @param   value *  @return  */public boolean gEtbit (Final string key ,final long value)  {if  (redistemplate !=  NULL)  {redistemplate.execute (new rediscallback<boolean> ()  {public Boolean  Doinredis (redisconnection connection) throws dataaccessexception {redisserializer<string>  serializer = getredisserializer (); Byte[] keys = serializer.serialize (key); Return connection.getbit (Keys, value);}});} Return false;} /** *   value  parameter Overwrite (overwrite) given  key  stored string value, from offset  offset  start  *   @param  key *  @param  offset *  @param  value *  @return  */ Public boolean setrange (final string key,final long offset,final string  value)  {if  (redistemplate != null)  {redistemplate.execute (new  Rediscallback<boolean> ()  {public boolean doinredis (RedisconNection connection) Throws dataaccessexception {redisserializer<string> serializer  = getredisserializer (); Byte[] keys = serializer.serialize (key); byte[] values  = serializer.serialize (value); Connection.setrange (keys,values,offset); return true;}); Return false;} /** *  returns a substring of the string value in  key , and the Intercept range of the string is determined by the  start  and  end  two offsets  * @ param key *  @param  startOffset *  @param  endOffset *  @return   */public byte[] getrange (Final string key,final long startoffset,final long  endoffset)  {if  (redistemplate != null)  {redistemplate.execute (new  Rediscallback<byte[]> ()  {public byte[] doinredis (redisconnection connection) throws  dataaccessexception {redisserializer<string> serializer = getredisserializer () ; Byte[] keys = serializer.serialize (key); Return connection.getrange (Keys,startoffset,endoffset);}}); Return null;} /** *  Delete Object  , dependency key */public void delete (String key)  {List<String>  list = new ArrayList<String> (); List.add (key);d elete (list);} /** *  Delete collection &nbsp, dependent on key collection  */@SuppressWarnings ("Unchecked") Private void delete (list< String> keys)  {redistemplate.delete ((K)  keys);} /** *  remove elements in the list that are equal to the parameters  value  equality  *  @param  keystr *, based on the value of the parameter  count    @param  count *  @param  valueStr *  @return  */public long lrem (Final string key, final long count, final string value)  {if   (Redistemplate != null)  {redistemplate.execute (new rediscallback<long> ()   {Public long doinredis (redisconnection connection) THROWS&NBSp;dataaccessexception {redisserializer<string> serializer = getredisserializer (); Byte[] keys = serializer.serialize (key); Byte[] values = serializer.serialize ( value); Return connection.lrem (keys, count, values);}}); return null;    }/** *  inserting one or more values  value  into the list  key  header  *  @param  keyStr *  @param  valueStr *  @return  */public long  lpush (Final string key, final string value)  {if  (redistemplate ! = null)  {redistemplate.execute (new rediscallback<long> ()  {public Long  Doinredis (redisconnection connection) throws dataaccessexception {redisserializer<string>  serializer = getredisserializer (); Byte[] keys = serializer.serialize (key); byte[ ] values = serializer.serialize (value);return Connection.lpush (keys, values);}});} return null;     The content is not complete ... Note      all source address    http://www.yoodb.com/article/display/1065

Java Spring and Redis operations package source code

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.