1, in the service package, respectively, set up a series of interfaces about the cache, classes, etc., encapsulated in a toolkit;
- Temporary cached Interfaces (Code section):
PackageCom.tools;Importjava.util.Date; Public InterfaceCachetemplet {//Set Add persistent cache (cache unique index, cache data) Public voidAddcache (Object key,object value); //set add cache, but need to have an expiration time limit of milliseconds Public voidAddcache (Object key,object value,long duration); //setting to add a cache requires a time limit of a date type Public voidAddcache (Object key,object value,date expire); //Get cache information, get cache information based on key PublicObject GetCache (object key); //Delete cache information and delete it based on key value Public voidDelcache (Object key);}
2. Establish a "factory for caching" to get the implementation of the cache, get the cache object from the factory, and then manipulate the cached information from the cache object (get it or delete it);
PackageCom.tools;//Cache Factory Public classCachefactory {//no parameter constructor Privatecachefactory () {}//Private member Variables PrivateCachetemplet Cachetemplet =NULL; //get JVM Cache (not finished)Public Cachetemplet GetCache () {...}
Get Cache
Public Cachetemplet GetCache (Cacheenum cacheenum) {...}
Redis connection Pooling
Private Redispool Redispool;
Next is the corresponding set method, which does not use the Get method,
public void Setredispool (Redispool redispool) {...}
private static cachefactory cachefactory = null;
Get Cache Factory
public static Cachefactory Getcachefactory () {...}
.
.
.
.
........
}
3, and then in the corresponding service implementation class, the specific use is as follows: Java code
Get the nearest hotel public list<map<string,string>> Putsale (final string product,final string Usertell) { // First from instantiating the cache temporary object Cachetemplet Cachetemplet = Cachefactory.getcachefactory (). GetCache (Cacheenum.redis);//temporary cache, get cache factory, get cache implementation //Create Cache entity class object list<map<string,string>> storeinre = new Arraylist<map<string, string>> (); map<string, string> e = new hashmap<string, string> (); E.put ("Yxwddh", YXWDDH); Hotel phone Storeinre.add (e);//Add cache information}
In a Java project, how to handle caching issues in "interfaces"