当前位置:网站首页>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 .
边栏推荐
- Data analysis - similarities and differences between C-end and b-end data analysis
- 如何高效的完成每日的任务?
- Theoretical speed calculation method of WiFi
- Postman斷言對應脚本的解釋
- GUN make (4) 规则的命令
- Multi type study of Worthington collagen protease
- Abnova CMV CISH probe solution
- Pointnet/Pointnet++学习
- tos cos dscp 区别和作用
- Code coverage test (I)
猜你喜欢

元气少女王钰洁 受邀担任第六季完美童模全球总决赛代言人

Dataframe extracts data from a column and converts it into a list

Abnova CMV CISH probe solution

Interpretation of script corresponding to postman assertion

分布式系统(二)分布式事务的理解

Viwi interface

Playful girl wangyixuan was invited to serve as the Promotion Ambassador for the global finals of the sixth season perfect children's model

二造实务案例答题技巧和举例汇总,满满都是精髓

cyclegan:unpaired image-to-image translation using cycle-consistent adversarial network

一分钟了解同步、异步、阻塞和非阻塞的区别
随机推荐
如何制定可实现中长期目标?
Chemical properties and application of trypsin
Energetic girl wangyujie was invited to be the spokesperson for the global finals of the sixth season perfect children's model
Exploring temporary information for dynamic network embedding
清甜女孩李斯霞 受邀担任第六季完美童模全球总决赛小主持人
Talking about interface test (I)
Gun make (7) execute make
cyclegan:unpaired image-to-image translation using cycle-consistent adversarial network
V4L2+QT视频优化策略
GUN make (1) 简介
PTA class a simulated sixth bomb: 1156-1159
Pre ++, post ++ and pre -- and post -- (+a, a++ and --a, a--)
PTA class a simulated 8th bomb: 1164-1167
通俗易懂C语言关键字static
Gun make (5) variables in makefile
LeetCode 41 ~ 50
弹性蛋白酶的用途和化学性质
Application and chemical properties of elastase
GUN make (7) 执行make
接口测试用例设计