Java integrated memcached, Redis prevents cache penetration

Source: Internet
Author: User

Download the relevant jar, install memcached, install Tutorial: http://www.runoob.com/memcached/memcached-install.html

Spring Configuration memcached

<bean id= "Memcachedpool" class= "Com.danga.MemCached.SockIOPool"
Factory-method= "getinstance" init-method= "Initialize" destroy-method= "ShutDown" >
<constructor-arg>
<value>memCachedPool</value>
</constructor-arg>
<property name= "Servers" >
<list>
<value>127.0.0.1:11211</value>
</list>
</property>
<property name= "Initconn" >
<value>20</value>
</property>
<property name= "Minconn" >
<value>10</value>
</property>
<property name= "Maxconn" >
<value>50</value>
</property>
<property name= "Maintsleep" >
<value>3000</value>
</property>
<property name= "Nagle" >
<value>false</value>
</property>
<property name= "Socketto" >
<value>100</value>
</property>
</bean>
<!--cache configuration--
<bean id= "memcachedclient" class= "Com.danga.MemCached.MemCachedClient" >
<constructor-arg>
<value>memCachedPool</value>
</constructor-arg>
</bean>

The principle code of the cache penetration, not much explanation:

Import Java.util.Date;
Import Javax.annotation.Resource;
Import Org.apache.commons.lang3.StringUtils;
Import Org.apache.commons.logging.Log;
Import Org.apache.commons.logging.LogFactory;
Import Org.springframework.stereotype.Service;
Import Com.alibaba.fastjson.JSON;
Import com.alibaba.fastjson.TypeReference;
Import com.danga.MemCached.MemCachedClient;
/**
* Prevent cache penetration (direct access to database via cache)
* Synchronized locks the current method, sorts all threads, and puts them on a method that affects the efficiency of the program.
* @author
*/
@Service
public class Memcachedservice {
Log log = Logfactory.getlog (this. getclass ());
@Resource
Private Memcachedclient memcachedclient;
/**
* Avoid cache penetration
* Read Cache
* @param key Cache key
* @param the expiration time of the expire cache
* @param type of Clazz cache
* @param locadback If the cache fails, how to get
* @return
*/
Public <T> T Findcache (String key,date expire,typereference<t> clazz,cacheloadback<t> locadback) {
String JSON = Memcachedclient.get (key) + "";
if (Stringutils.isnotempty (JSON) &&!json.equalsignorecase ("null")) {
Log.info ("------Read cache data ==================" +key);
Return Json.parseobject (JSON, clazz);
}else{
Synchronized (this) {
JSON = Memcachedclient.get (key) + "";
if (Stringutils.isnotempty (JSON) &&!json.equalsignorecase ("null")) {
Return Json.parseobject (JSON, clazz);
}
T result = Locadback.load ();
if (result! = null) {
Memcachedclient.set (Key, Json.tojson (result), expire);
}
return result;
}
}
}
}

Business Implementation Layer Code

@Resource
Private Memcachedservice Memcachedservice;

@Override
Public list<sensordatavo1> getsensordatavo2s (String sensorid,string passway,int curpage,int pageSize) {
Use the method name as the key value
String method = Thread.CurrentThread (). Getstacktrace () [1].getmethodname ();
Return Memcachedservice.findcache (Method+sensorid+passway+curpage+pagesize,dateutils.addminutes (New Date (), 10), New Typereference<list<sensordatavo1>> () {},new cacheloadback<list<sensordatavo1>> () {
@Override
Public list<sensordatavo1> load () {
Return Sensordatadao.getsensordatavo1s (sensorid,passway,curpage,pagesize);
}
});
}

Interface layer:

Public interface Cacheloadback<t> {
Public T load ();
}

Java integrated memcached, Redis prevents cache penetration

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.