1、Pom.xml文件
javax.cache
cache-api
org.ehcache
ehcache
${ehcache3.version}
2、Config/application.yml 添加配置
项目名称下的配置
cache:
jcache.config: classpath:ehcache.xml
3、resources/ehcache.xml创建文件,添加内容
##缓存名称 --可注释本行
120
1
5000
com.wiwide.tpapi.server.config.CacheEventLogger
ASYNCHRONOUS
UNORDERED
CREATED
UPDATED
EXPIRED
REMOVED
EVICTED
1000
100
4、读取配置的类
package ….config; 简化
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.concurrent.ConcurrentMapCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableCaching
public class CacheConfiguration {
@Bean
public CacheManager cacheManager() {
return new ConcurrentMapCacheManager();
}
}
5、创建缓存的日志类
package com.wiwide.tpapi.server.config;
import lombok.extern.slf4j.Slf4j;
import org.ehcache.event.CacheEvent;
import org.ehcache.event.CacheEventListener;
@Slf4j
public class CacheEventLogger implements CacheEventListener
6、sql接口类中添加查询添加缓存和删除移除缓存配置
操作MySQL
@CacheResult(cacheName = "csid")
@Select("")
String getCsid(
@Param("bkey") int bkey,
@Param("shop_id") int shopId);
@CacheRemove(cacheName = "csid")
@Insert("")
int upsertShopMapping(
@Param("bkey") int bkey,
@Param("shop_id") int shopId,
@Param("mapping_id") @CacheValue String mappingId);
@CacheRemove(cacheName = "csid")
@Delete("")
int deleteShopMapping(
@Param("bkey") int bkey,
@Param("shop_id") int shopId);
7、MongoDB接口中添加查询添加缓存和删除移除缓存配置
操作MongoDB
package com.wiwide.tpapi.server.visioncenter.domain;
import com.wiwide.tpapi.server.config.cache.MemberInfoCacheKeyGenerator;
import javax.cache.annotation.CacheRemove;
import javax.cache.annotation.CacheResult;
import org.springframework.data.mongodb.repository.MongoRepository;
public interface MemberInfoRepository extends MongoRepository {
MemberInfo findByBusinessKeyAndMemberId(int businessKey, String memId);
@CacheResult(cacheName = "faceMember", cacheKeyGenerator = MemberInfoCacheKeyGenerator.class)
MemberInfo findTop1ByMemberPersonFaceid(String faceid);
@CacheRemove(cacheName = "faceMember", cacheKeyGenerator = MemberInfoCacheKeyGenerator.class)
S save(S entity);
@CacheRemove(cacheName = "faceMember", cacheKeyGenerator = MemberInfoCacheKeyGenerator.class)
void delete(MemberInfo entity);
}
package com.wiwide.tpapi.server.config.cache;
import com.wiwide.tpapi.server.visioncenter.domain.MemberInfo;
import java.lang.annotation.Annotation;
import javax.cache.annotation.CacheInvocationParameter;
import javax.cache.annotation.CacheKeyGenerator;
import javax.cache.annotation.CacheKeyInvocationContext;
import javax.cache.annotation.GeneratedCacheKey;
import lombok.Data;
import lombok.val;
public class MemberInfoCacheKeyGenerator implements CacheKeyGenerator {
@Override
public GeneratedCacheKey generateCacheKey(
CacheKeyInvocationContext extends Annotation> cacheKeyInvocationContext) {
if (1 != cacheKeyInvocationContext.getKeyParameters().length) {
return null;
}
CacheInvocationParameter keyParameter = cacheKeyInvocationContext.getKeyParameters()[0];
Object value = keyParameter.getValue();
String faceid;
if (value instanceof MemberInfo) {
// MemberInfo for save/update/delete
val member = (MemberInfo)value;
if (null == member.getMember() || null == member.getMember().getPerson()) {
return null;
}
faceid = member.getMember().getPerson().getFaceid();
} else if (value instanceof String) {
// String(faceid) for query
faceid = (String)value;
} else {
// Unsupported
String msg = String.format("Cache key type wrong. Required %s or String, found %s.",
MemberInfo.class.getName(), value.getClass().getName());
throw new ClassCastException(msg);
}
return new MemberInfoFaceIdKey(faceid);
}
@Data
private static class MemberInfoFaceIdKey implements GeneratedCacheKey {
private String faceid;
MemberInfoFaceIdKey(String faceid) {
this.faceid = faceid;
}
}
}