开发含有缓存操作的服务

匿名 (未验证) 提交于 2019-12-02 23:37:01

读取,写入缓存

package com.shtel.paas.service.cache.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import com.shtel.paas.sdk.core.PaasBaseResponse;
import com.shtel.paas.sdk.core.RefreshableRestController;
import com.shtel.paas.sdk.core.cache.ctgcache.ICtgCacheManager;
import com.shtel.paas.sdk.core.metrics.PaasMethodMetrics;
import com.shtel.paas.service.cache.domain.CacheRespContent;
import com.shtel.paas.service.cache.domain.SaveToCacheRequest;
//使用@EnableCtgCache注解,系统会自动装配一个ICtgCacheManager对象,后续使用@Autowired注入后即可进行缓存相关操作
@RefreshableRestController
@EnableCtgCache
public class CtgCacheService {

@Autowired ICtgCacheManager cache;  @PaasMethodMetrics @GetMapping("/getbykey/{key}") public PaasBaseResponse getbykey(@PathVariable String key) {     PaasBaseResponse rep = new PaasBaseResponse();     CacheRespContent crc=new CacheRespContent();     crc.setKey(key);     crc.setVal(cache.get(key));     rep.setContent(crc);     return rep; }  @PaasMethodMetrics @PostMapping("/savetocache") public PaasBaseResponse savetocache(@RequestBody SaveToCacheRequest req) {     PaasBaseResponse rep = new PaasBaseResponse();     cache.set(req.getKey(), req.getVal());      return rep; } 

}
缓存配置

ctgcache:
username: app
password: “ideal123!@#”
groupid: “group.ord.order.te” #缓存组
zKhost: “10.145.196.106:2181,10.145.196.107:2181,10.145.196.108:2181” #缓存相关zk地址
########缓存默认配置,基本不变##########
timeout: 10000
zkPath: “/apps/cache”
zkTimeout: 5000
redisProxyTimeoutValue: 3
cliConfigValue: “client_01”
########缓存默认配置,基本不变##########
authEnabled: false #服务端是否开启身份验证

20190222 新增池化缓存配置

usePool: true # 是否启用连接池方式连接缓存,默认false,开启后需要配置conn,pool相关信息

20190422 新增缓存失败是否重试开关

useRetry: false #缓存操作失败后,是否启用重试,默认false
retryTimes: 3 #缓存操作异常后重试次数,默认3次
retryDelays: 0 #每次重试之间的间隔,单位毫秒,默认0

20190405 新增原生redis后端开关

useRedis: true #是否启用原生redis后端,启用后需要配置原生redis相关连接信息
conn:
password: “app#app@app” #“用户#密码”必填
database: 254 #桶位 (缓存组) 必填
period: 3000 #后台监控执行周期,毫秒,默认3秒
connectionTimeout: 2000 #连接超时,单位毫秒,默认2000
soTimeout: 2000 #读取超时,单位毫秒,默认2000
monitorTimeout: 200 #监控命令超时时间,毫秒,默认500
monitorErrorNum: 3 #监控连续出错几次,将接入机节点判断为失效,默认3次
monitorLog: true #是否开启后台监控日志,默认开启
nodes: “10.10.10.10:1234” #接入机地址列表 必填
pool:
maxTotal: 20 #最大连接数,默认20
maxIdle: 16 #最大空闲连接数,默认16
minIdle: 8 #保持的最小空闲连接数,默认8
maxWaitMillis: 3000 #获取连接时的最大等待毫秒数,如果超时就抛异常, 小于零:阻塞不确定的时间, 默认-1
testWhileIdle: true #在空闲时检查有效性,默认true
minEvictableIdleTimeMillis: 60 #逐出连接的最小空闲时间(毫秒),默认60秒
timeBetweenEvictionRunsMillis: 30 #逐出扫描的时间间隔(毫秒) 如果为负数,则不运行逐出线程, 默认30秒
numTestsPerEvictionRun: -1 #每次逐出检查时 逐出的最大数目百分比,如果是负数,则为1/abs(n),默认为-1

#原生redis连接信息
spring:
redis:
host: 10.145.208.181 #redis服务器ip,默认localhost,单点redis连接,集群还是单点按实际环境只要配一类
port: 16379 #redis服务器端口,默认6379
password: 123456 #redis认证密码
timeout: 60000 #连接超时,单位毫秒,默认值60000=60秒
database: 0 #redis数据库id,即原来ctgcache的桶位,默认0
pool:
maxIdle: 10 #最大空闲连接,默认8
minIdle: 10 #最小空闲连接,默认0
maxActive: 10 #最大连接,默认8
maxWait: 3000 #从池中获取连接等待时间,默认-1
cluster: #集群配置,集群还是单点按实际环境只要配一类
nodes: 10.145.208.181:7000,10.145.208.181:7001,10.145.208.181:7002 #集群节点

文章来源: https://blog.csdn.net/weixin_39810017/article/details/90755399
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!