Lforumcache
Package Com. javaeye. lonlysky. lforum. cache; Import Java.net. url; Import Java. util. List; Import Net. SF. ehcache. cache; Import Net. SF. ehcache. cachemanager; Import Net. SF. ehcache. element; Import Org. slf4j. Logger; Import Org. slf4j. loggerfactory; /*** Lforum Cache Management *** @ author Huang Lei **/ Public Class Lforumcache { Private Static Final Logger logger = loggerfactory. getlogger (lforumcache. Class ); Private Static Object lockobject = New Object (); Private Static Lforumcache instance = Null ; Public Static Final String cache_name ="Lforumcaches "; Private Lforumcache (){} /*** Get and change the instance ** @ return lforumcache instance */ Public Static Lforumcache getinstance (){ If (Instance = Null ){ Synchronized (Lockobject) {logger.info (" Create a new cache instance "); Instance = New Lforumcache ();}} Return Instance ;}/*** Obtain the Cache Management class ** @ return Cache Management class */ Public Cachemanager getcachemanager () {URL url = getclass (). getresource (" Ehcache-hibernate.xml "); Return Cachemanager. Create (URL );} /*** Obtain the Forum cache ** @ return cache */ Public Cache getcache (){ Return Getcachemanager (). getcache (cache_name );} /*** Add cache object ** @ Param key cache key * @ Param cacheobj cache object */ Public Void Addcache (string key, object cacheobj) {getcache (). Put (New Element (Key, cacheobj )); If (Logger. isdebugenabled () {logger. debug (" Add cache objects whose key is {}. The current cache is {} ", Key, getcache (). getsize ());}} /*** Get cache object *** @ Param key cache key * @ return cache object */ Public Object getcache (string key) {element = getcache (). Get (key ); If (Element = Null ){ Return Null ;} If (Logger. isdebugenabled () {logger. debug ("Get cache object with key {} ", Key );} Return (Object) element. getvalue ();} /*** Get the cache object of the specified type ** @ Param <t> specify the type * @ Param key cache key * @ Param classz class * @ return specify the type of cache object */ @ Suppresswarnings (" Unchecked ") Public <T> T getcache (string key, class <t> classz ){ If (Getcache (key) = Null ){ Return Null ;} If (Logger. isdebugenabled () {logger. debug ("Obtain the cache object whose key is {} and type is {}. ", Key, classz. getname ());} Return (T) getcache (key );} /*** Obtain the list cache of the specified type ** @ Param <t> specify the type * @ Param key cache key * @ Param classz type class * @ return specify the type list cache */ @ Suppresswarnings (" Unchecked ") Public <T> List <t> getlistcache (string key, class <t> classz ){ If (Getcache (key) = Null ){ Return Null ;} If (Logger. isdebugenabled () {logger. debug (" Obtain the list cache with the key {} and type {}. ", Key, classz. getname ());} Return (List <t>) getcache (key );} /*** Remove the specified key cache object ** @ Param key cache key */ Public Void Removecache (string key ){ If (Getcache (key )! = Null ) {Getcache (). Remove (key ); If (Logger. isdebugenabled () {logger. debug (" Remove cache objects whose key is {} ", Key );}}}}
Use lforumcache cache in various manager operations
Package Com. javaeye. lonlysky. lforum. Service; Import Java. util. List; Import Org. hibernate. sessionfactory; Import Org. slf4j. Logger; Import Org. slf4j. loggerfactory; Import Org. springframework. Beans. Factory. annotation. autowired; Import Org. springframework. stereotype. Service; Import Org. springframework. transaction. annotation. Transactional; Import Org. springside. modules. Orm. hibernate. simplehibernatetemplate;Import Com. javaeye. lonlysky. lforum. cache. lforumcache; Import Com. javaeye. lonlysky. lforum. entity. Forum. announcements; /***** Forum announcement operation class ** @ author Huang Lei **/ @ Service @ transactional Public Class Announcementmanager { Private Final Static Logger logger = loggerfactory. getlogger (announcementmanager. Class ); Private Simplehibernatetemplate <announcements, integer> announcementdao; @ autowiredPublic Void Setsessionfactory (sessionfactory) {announcementdao = New Simplehibernatetemplate <announcements, integer> (sessionfactory, announcements. Class );} /*** Get the announcement list for all specified time periods * @ Param starttime start time * @ Param endtime end time * @ return announcement list */ @ Suppresswarnings (" Unchecked ") Public List <announcements> getannouncementlist (string starttime, string endtime) {list <announcements> announcemenlist = lforumcache. getinstance (). getlistcache (" Announcementlist ", Announcements.Class ); If (Announcemenlist = Null ) {Announcemenlist = announcementdao. Find (" From announcements where starttime <=? And endtime >=? Order by displayorder, Id DESC ", Starttime, endtime ); If (Logger. isdebugenabled () {logger. debug (" Get time range "+ Starttime +" - "+ Endtime +" Announcement list ");} Lforumcache. getinstance (). Addcache (" Announcementlist ", Announcemenlist );}Return Announcemenlist ;} /*** Get the list of the first n announcements within all specified time periods * @ Param starttime start time * @ Param endtime end time * @ Param maxcount maximum number of records, if the value is less than 0, all * @ return announcement lists */ @ Suppresswarnings (" Unchecked ") Public List <announcements> getsimplifiedannouncementlist (string starttime, string endtime, Int Maxcount) {list <announcements> announcemenlist = lforumcache. getinstance (). getlistcache (" Simplifiedannouncementlist ", Announcements. Class ); If (Announcemenlist =Null ) {Announcemenlist = announcementdao. createquery (" From announcements where starttime <=? And endtime >=? Order by displayorder, Id DESC ", Starttime, endtime). setmaxresults (maxcount). List (); lforumcache. getinstance (). addcache (" Simplifiedannouncementlist ", Announcemenlist );} Return Announcemenlist ;}}