当前位置:网站首页>The first intimate contact of caching technology
The first intimate contact of caching technology
2022-06-26 02:00:00 【Murong Tianyu】
I heard about caching before , Because I haven't had a deep understanding of , So I feel very abstruse , Only know hibernate Embedded cache technology , He can speed up the query , I haven't really written it myself . Today, there is an interface for calculating premium , Because there are many fixed parameters involved in the calculation , Get the parameters first for each request , Calculate again , The computational logic is not complicated . It was previously stored in the database , Because you need to query the database many times , Resulting in slow computing speed , User complaints , So we need to optimize it , Reduce query time . Well, that's all for the background , Let's start to see how cache is used !
First step , Create an aspect oriented interception processing class , Intercept all interfaces for premium calculation
package com.travelsky.umetrip.insure.util.common;
import org.apache.commons.lang.StringUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import cn.co.msky.core.cache.memcache.MemCacheWrap;
/**
* @author murong
* @version Creation time :2014-12-12 Afternoon 2:13:07
* memCache Cache handling
*/
@Aspect
@Component
public class CacheInterceptor {
/**
* @Pointcut : Indicates a specified entry point execution() Grammatical norms first “*” Represents any return result type
* “cn.itcast.service.impl.PersonServiceBean”: Means to intercept this class ,
* If it is cn.itcast.service..*.*: Indicates to package cn.itcast.service And what's in the bag
* All methods of some classes are intercepted , (..) Indicates the parameters of the interception method
*/
@SuppressWarnings("unused")
@Pointcut("execution(* com.travelsky.umetrip.insure.dao.premium.impl.PremiumDaoImpl.*(..))")
private void anyMethod() {
}// Declare an entry point
@Around("anyMethod()")
public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable {
String open = PropertiesLoader.load("cache_open");
Object obj = null;
// Read cache status parameters , If it is on
if (!StringUtils.isBlank(open) && "true".equals(open)) {
try {
Object target = pjp.getTarget();
String key = getObjectKey(pjp);
int cacheTime = getCacheTime(pjp);
Object memCacheGet = MemCacheWrap.MemCacheGet(key);
LogUtil.info(this.getClass(), "get key="+key+" value="+memCacheGet);
if(memCacheGet == null)
{
obj = pjp.proceed();
MemCacheWrap.MemCachePut(key, obj, cacheTime);
Object memCacheG = MemCacheWrap.MemCacheGet(key);
LogUtil.info(this.getClass(), "get key="+key+" value="+memCacheG);
LogUtil.info(this.getClass(), "put key="+key+" value="+obj);
}else {
obj = memCacheGet;
}
} catch (Exception e) {
LogUtil.error(this.getClass(), "memcache doBasicProfiling Exception!" + pjp,e);
}
}else {
obj = pjp.proceed();
}
return obj;
}
private String getObjectKey(ProceedingJoinPoint pjp) {
// TODO Auto-generated method stub
String project = "UmetripInsure";
String key = "";
Object target = pjp.getTarget();
String className = pjp.getTarget().getClass().getSimpleName();
String methodName = pjp.getSignature().getName();
Object[] args = pjp.getArgs();
key = project + "$"+className+"$"+methodName+"$"+PropertiesLoader.paraToString(args);
return key;
}
private int getCacheTime(ProceedingJoinPoint pjp){
String className = pjp.getTarget().getClass().getSimpleName();
String methodName = pjp.getSignature().getName();
String key = className+"_"+methodName+"_cachetime";
String cachetime =PropertiesLoader.load(key);
if (cachetime == null) {
cachetime =PropertiesLoader.load("default_cachetime");
}
return Integer.parseInt(cachetime);
}
}
Configure the current class to xml Of bean in , This is not a code .
The second step , Add the configured in the configuration file memcached.xml
<?xml version="1.0" encoding="UTF-8"?>
<memcached>
<!-- one -->
<client name="mskymemcache1" compressEnable="true" defaultEncoding="UTF-8" socketpool="pool0">
<errorHandler>com.alisoft.xplatform.asf.cache.memcached.MemcachedErrorHandler</errorHandler>
</client>
<client name="mskymemcache1bk" compressEnable="true" defaultEncoding="UTF-8" socketpool="pool0bk">
<errorHandler>com.alisoft.xplatform.asf.cache.memcached.MemcachedErrorHandler</errorHandler>
</client>
<socketpool name="pool0" failover="true" initConn="5" minConn="5" maxConn="250" maintSleep="0"
nagle="false" socketTO="3000" aliveCheck="true">
<servers>122.119.120.15:11211</servers>
</socketpool>
<socketpool name="pool0bk" failover="true" initConn="5" minConn="5" maxConn="250" maintSleep="0"
nagle="false" socketTO="3000" aliveCheck="true">
<servers>122.119.120.16:11211</servers>
</socketpool>
<cluster name="cluster1" mode="standby">//mode = active,standby
<memCachedClients>mskymemcache1,mskymemcache1bk</memCachedClients>
</cluster>
<!-- two -->
<client name="mskymemcache2" compressEnable="true" defaultEncoding="UTF-8" socketpool="pool2">
<errorHandler>com.alisoft.xplatform.asf.cache.memcached.MemcachedErrorHandler</errorHandler>
</client>
<client name="mskymemcache2bk" compressEnable="true" defaultEncoding="UTF-8" socketpool="pool2bk">
<errorHandler>com.alisoft.xplatform.asf.cache.memcached.MemcachedErrorHandler</errorHandler>
</client>
<socketpool name="pool2" failover="true" initConn="5" minConn="5" maxConn="250" maintSleep="0"
nagle="false" socketTO="3000" aliveCheck="true">
<servers>122.101.120.15:1111</servers>
</socketpool>
<socketpool name="pool2bk" failover="true" initConn="5" minConn="5" maxConn="250" maintSleep="0"
nagle="false" socketTO="3000" aliveCheck="true">
<servers>122.101.120.16:1111</servers>
</socketpool>
<cluster name="cluster2" mode="standby">//mode = active,standby
<memCachedClients>mskymemcache2, mskymemcache2bk</memCachedClients>
</cluster>
</memcached>The third step , In your properties Add two configurations to the file
cache_open=true<span style="font-family:Microsoft YaHei;">// Open cache or not </span>
default_cachetime=86400<span style="font-family:Microsoft YaHei;">// Cache save time ( Company : second )</span>
Caching technology is done .
边栏推荐
- 宁要一个完成,不要千万个开始(转载自豆瓣)
- Web Testing
- 前置++,后置++与前置--与后置--(++a,a++与--a,a--)
- 求n乘阶之和
- Abnova丨抗GBA单克隆抗体解决方案
- GUN make (1) 简介
- 阳光男孩陈颢天 受邀担任第六季完美童模全球总决赛代言人
- Distributed systems (II) understanding of distributed transactions
- About vs scanf, 'scanf' appears: this function or variable may be unsafe Solutions to the problem of consumer usi
- Chrome浏览器开发者工具使用
猜你喜欢

Redis7.0 installation steps

A lost note for konjaku beginner

recv & send

甜酷少女金书伊 受邀担任第六季完美童模全球总决赛代言人

Assertion of postman interface test

Talking about interface test (I)

On the difference between strlen and sizeof

Abnova丨抗GBA单克隆抗体解决方案

One stop solution EMQ for hundreds of millions of communication of Internet of things

论文阅读 Exploring Temporal Information for Dynamic Network Embedding
随机推荐
甜酷少女金书伊 受邀担任第六季完美童模全球总决赛代言人
Distributed systems (II) understanding of distributed transactions
Dataframe to list
Web Testing
Mot - clé C facile à comprendre statique
Use of redis
JQ user defined attribute value
Multi type study of Worthington collagen protease
王老吉药业“关爱烈日下最可爱的人”公益活动在杭启动
如何高效的完成每日的任务?
LeetCode 31 ~ 40
memory leak check tools 详解
如何为政企移动办公加上一道“安全锁”?
Dataframe extracts data from a column and converts it into a list
shell curl 执行脚本,带传参数,自定义参数
ActivityManager kill reason
App test (I)
readv & writev
Energetic girl wangyujie was invited to be the spokesperson for the global finals of the sixth season perfect children's model
On the difference between strlen and sizeof