- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章Spring AOP如何整合redis(注解方式)实现缓存统一管理详解由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
前言 。
项目使用redis作为缓存数据,但面临着问题,比如,项目a,项目b都用到redis,而且用的redis都是一套集群,这样会带来一些问题.
问题:比如项目a的开发人员,要缓存一些热门数据,想到了redis,于是乎把数据放入到了redis,自定义一个缓存key:hot_data_key,数据格式是项目a自己的数据格式,项目b也遇到了同样的问题,也要缓存热门数据,也是hot_data_key,数据格式是项目b是自己的数据格式,由于用的都是同一套redis集群,这样key就是同一个key,有的数据格式适合项目a,有的数据格式适合项目b,会报错的,我们项目中就遇到这样的一个错误,找不到原因,结果就是两个平台用到了同一key,很懊恼.
解决方式:
1、弄一个常量类工程,所有的redis的key都放入到这个工程里,加上各自的平台标识,这样就不错错了 。
2、spring aop结合redis,再相应的service层,加上注解,key的规范是包名+key名,这样就不错重复了 。
思路:
1、自定义注解,加在需要缓存数据的地方 。
2、spring aop 结合redis实现 。
3、spel解析注解参数,用来得到响应的注解信息 。
4、redis的key:包名+key 防止redis的key重复 。
实现如下:
项目准备,由于是maven项目,需要引入相关的包 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
fastjson包
<dependency>
<groupid>com.alibaba</groupid>
<artifactid>fastjson</artifactid>
<version>${com.alibaba.fastjson}</version>
</dependency>
spring-data-redis
<dependency>
<groupid>org.springframework.data</groupid>
<artifactid>spring-data-redis</artifactid>
<version>${spring.redis.version}</version>
</dependency>
<dependency>
<groupid>redis.clients</groupid>
<artifactid>jedis</artifactid>
<version>${jedis.redis.clients.version}</version>
</dependency>
|
还有一些必备的就是spring工程相关的包 。
1、自定义注解 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
import
java.lang.annotation.elementtype;
import
java.lang.annotation.retention;
import
java.lang.annotation.retentionpolicy;
import
java.lang.annotation.target;
import
java.util.concurrent.timeunit;
/**
* 缓存注解
*
* @author shangdc
*
*/
@target
({elementtype.method})
@retention
(retentionpolicy.runtime)
public
@interface
rediscache {
/**
* 缓存key的名称
* @return
*/
string key();
/**
* key是否转换成md5值,有的key是整个参数对象,有的大内容的,比如一个大文本,导致redis的key很长
* 需要转换成md5值作为redis的key
* @return
*/
boolean
keytransformmd5()
default
false
;
/**
* key 过期日期 秒
* @return
*/
int
expiretime()
default
60
;
/**
* 时间单位,默认为秒
* @return
*/
timeunit dateunit()
default
timeunit.seconds;
}
|
2、定义切点pointcut 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
|
/**
* redis 缓存切面
*
* @author shangdc
*
*/
public
class
rediscacheaspect {
//由于每个人的环境,日志用的不一样,怕报错,就直接关掉了此日志输出,如有需要可加上
//private logger log = logger.getlogger(rediscacheaspect.class);
/**
* 这块可配置,每个公司都要自己的缓存配置方式,到时候可配置自己公司所用的缓存框架和配置方式
*/
@resource
(name =
"redistemplate"
)
private
valueoperations<string, string> valueoperations;
/**
* 具体的方法
* @param jp
* @return
* @throws throwable
*/
public
object cache(proceedingjoinpoint jp, rediscache cacheable)
throws
throwable{
// result是方法的最终返回结果
object result =
null
;
// 得到类名、方法名和参数
object[] args = jp.getargs();
//获取实现类的方法
method method = getmethod(jp);
//注解信息 key
string key = cacheable.key();
//是否转换成md5值
boolean
keytransformmd5 = cacheable.keytransformmd5();
//----------------------------------------------------------
// 用spel解释key值
//----------------------------------------------------------
//解析el表达式后的的redis的值
string keyval = springexpressionutils.parsekey(key, method, jp.getargs(), keytransformmd5);
// 获取目标对象
object target = jp.gettarget();
//这块是全路径包名+目标对象名 ,默认的前缀,防止有的开发人员乱使用key,乱定义key的名称,导致重复key,这样在这加上前缀了,就不会重复使用key
string target_class_name = target.getclass().getname();
stringbuilder redis_key =
new
stringbuilder(target_class_name);
redis_key.append(keyval);
//最终的redis的key
string redis_final_key = redis_key.tostring();
string value = valueoperations.get(redis_final_key);
if
(value ==
null
) {
//这块是判空
// 缓存未命中,这块没用log输出,可以自定义输出
system.out.println(redis_final_key +
"缓存未命中缓存"
);
// 如果redis没有数据则执行拦截的方法体
result = jp.proceed(args);
//存入json格式字符串到redis里
string result_json_data = jsonobject.tojsonstring(result);
system.out.println(result_json_data);
// 序列化结果放入缓存
valueoperations.set(redis_final_key, result_json_data, getexpiretimeseconds(cacheable), timeunit.seconds);
}
else
{
// 缓存命中,这块没用log输出,可以自定义输出
system.out.println(redis_final_key +
"命中缓存,得到数据"
);
// 得到被代理方法的返回值类型
class
<?> returntype = ((methodsignature) jp.getsignature()).getreturntype();
//拿到数据格式
result = getdata(value, returntype);
}
return
result;
}
/**
* 根据不同的class返回数据
* @param value
* @param clazz
* @return
*/
public
<t> t getdata(string value,
class
<t> clazz){
t result = jsonobject.parseobject(value, clazz);
return
result;
}
/**
* 获取方法
* @param pjp
* @return
* @throws nosuchmethodexception
*/
public
static
method getmethod(proceedingjoinpoint pjp)
throws
nosuchmethodexception {
// --------------------------------------------------------------------------
// 获取参数的类型
// --------------------------------------------------------------------------
object[] args = pjp.getargs();
class
[] argtypes =
new
class
[pjp.getargs().length];
for
(
int
i =
0
; i < args.length; i++) {
argtypes[i] = args[i].getclass();
}
string methodname = pjp.getsignature().getname();
class
<?> targetclass = pjp.gettarget().getclass();
method[] methods = targetclass.getmethods();
// --------------------------------------------------------------------------
// 查找class<?>里函数名称、参数数量、参数类型(相同或子类)都和拦截的method相同的method
// --------------------------------------------------------------------------
method method =
null
;
for
(
int
i =
0
; i < methods.length; i++) {
if
(methods[i].getname() == methodname) {
class
<?>[] parametertypes = methods[i].getparametertypes();
boolean
issamemethod =
true
;
// 如果相比较的两个method的参数长度不一样,则结束本次循环,与下一个method比较
if
(args.length != parametertypes.length) {
continue
;
}
// --------------------------------------------------------------------------
// 比较两个method的每个参数,是不是同一类型或者传入对象的类型是形参的子类
// --------------------------------------------------------------------------
for
(
int
j =
0
; parametertypes !=
null
&& j < parametertypes.length; j++) {
if
(parametertypes[j] != argtypes[j] && !parametertypes[j].isassignablefrom(argtypes[j])) {
issamemethod =
false
;
break
;
}
}
if
(issamemethod) {
method = methods[i];
break
;
}
}
}
return
method;
}
/**
* 计算根据cacheable注解的expire和dateunit计算要缓存的秒数
* @param cacheable
* @return
*/
public
int
getexpiretimeseconds(rediscache rediscache) {
int
expire = rediscache.expiretime();
timeunit unit = rediscache.dateunit();
if
(expire <=
0
) {
//传入非法值,默认一分钟,60秒
return
60
;
}
if
(unit == timeunit.minutes) {
return
expire *
60
;
}
else
if
(unit == timeunit.hours) {
return
expire *
60
*
60
;
}
else
if
(unit == timeunit.days) {
return
expire *
60
*
60
*
24
;
}
else
{
//什么都不是,默认一分钟,60秒
return
60
;
}
}
}
|
3、spring相关配置 。
由于是公司的项目,所有包就的路径就去掉了 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<!-- aop配置,切面类 .rediscacheaspect类 bean-->
<bean id=
"rediscacheaspect"
class
=
"包.rediscacheaspect"
>
</bean>
<!-- 拦截所有指定 包和指定类型下的 下所有的方法 ,你是想这个在哪些包下可以实现-->
<aop:config proxy-target-
class
=
"true"
>
<aop:aspect ref=
"rediscacheaspect"
>
<aop:pointcut id=
"rediscacheaoppointcut"
expression=
"(execution(* 包.business.web.service.*.*(..)) and @annotation(cacheable))"
/>
<!-- 环绕 ,命中缓存则直接放回缓存数据,不会往下走,未命中直接放行,直接执行对应的方法-->
<aop:around pointcut-ref=
"rediscacheaoppointcut"
method=
"cache"
/>
</aop:aspect>
</aop:config>
|
4、工具类 spel 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
/**
* spring el表达式
*
* @author shangdc
*
*/
public
class
springexpressionutils {
/**
* 获取缓存的key key 定义在注解上,支持spel表达式 注: method的参数支持javabean和map
* method的基本类型要定义为对象,否则没法读取到名称
*
* example1: phone phone = new phone(); "#{phone.cpu}" 为对象的取值 、
* example2: map apple = new hashmap(); apple.put("name","good apple"); "#{apple[name]}" 为map的取值
* example3: "#{phone.cpu}_#{apple[name]}"
*
*
* @param key
* @param method
* @param args
* @return
*/
public
static
string parsekey(string key, method method, object[] args,
boolean
keytransformmd5) {
// 获取被拦截方法参数名列表(使用spring支持类库)
localvariabletableparameternamediscoverer u =
new
localvariabletableparameternamediscoverer();
string[] paranamearr = u.getparameternames(method);
// 使用spel进行key的解析
expressionparser parser =
new
spelexpressionparser();
// spel上下文
standardevaluationcontext context =
new
standardevaluationcontext();
// 把方法参数放入spel上下文中
for
(
int
i =
0
; i < paranamearr.length; i++) {
context.setvariable(paranamearr[i], args[i]);
}
parsercontext parsercontext =
new
templateparsercontext();
// ----------------------------------------------------------
// 把 #{ 替换成 #{# ,以适配spel模板的格式
// ----------------------------------------------------------
//例如,@注解名称(key="#{player.username}",expire = 200)
//#{phone[cpu]}_#{phone[ram]}
//#{player.username}_#{phone[cpu]}_#{phone[ram]}_#{pageno}_#{pagesize}
object returnval = parser.parseexpression(key.replace(
"#{"
,
"#{#"
), parsercontext).getvalue(context, object.
class
);
//这块这么做,是为了object和string都可以转成string类型的,可以作为key
string return_data_key = jsonobject.tojsonstring(returnval);
//转换成md5,是因为redis的key过长,并且这种大key的数量过多,就会占用内存,影响性能
if
(keytransformmd5) {
return_data_key = md5util.digest(return_data_key);
}
return
returnval ==
null
?
null
: return_data_key;
}
}
|
5、redis相关配置 。
重要的是自己的redis配置,可能跟我的不太一样,用自己的就好 。
1
2
3
4
|
<!-- redistemplate defination -->
<bean id=
"redistemplate"
class
=
"org.springframework.data.redis.core.stringredistemplate"
>
<property name=
"connectionfactory"
ref=
"jedisconnectionfactory"
/>
</bean>
|
测试 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
public
class
rediscachetest {
@test
public
void
test() {
applicationcontext context =
new
classpathxmlapplicationcontext(
"classpath:spring/spring-applicationcontext.xml"
);
rediscacheaopservice rediscacheaopservice = (rediscacheaopservice) context.getbean(
"rediscacheaopservice"
);
api api = rediscacheaopservice.getapi(1l);
system.out.println(api.getclass());
system.out.println(jsonobject.tojsonstring(api));
apiparam param =
new
apiparam();
param.setid(2l);
param.setapiname(
"短信服务接口数据"
);
// system.out.println("tostring:" + param.tostring());
// system.out.println(md5util.digest(param.tostring()));
api api_1 = rediscacheaopservice.getapibyparam(param);
system.out.println(api_1.getclass());
system.out.println(jsonobject.tojsonstring(api_1));
}
}
|
测试打印信息:
大体思路是这样,需要自己动手实践,不要什么都是拿过来直接copy,使用,整个过程都不操作,也不知道具体的地方,该用什么,自己实际操作,可以得到很多信息.
辅助信息类:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
|
public
class
api
implements
serializable {
private
static
final
long
serialversionuid = 1l;
/**
*
* 自增主键id
*/
private
long
id;
/**
*
* api名称
*/
private
string apiname;
/**
*
* api描述
*/
private
string apidescription;
/**
*
* 有效时间
*/
private
integer valid;
/**
* 处理类
*/
private
string handlerclass;
/**
*
*
*/
private
date updatetime;
/**
*
*
*/
private
date createtime;
public
api() {
}
public
string tostring() {
return
"id:"
+ id +
", apiname:"
+ apiname +
", apidescription:"
+ apidescription +
", valid:"
+ valid +
", updatetime:"
+ updatetime +
", createtime:"
+ createtime;
}
public
long
getid() {
return
this
.id;
}
public
void
setid(
long
id) {
this
.id = id;
}
public
string getapiname() {
return
this
.apiname;
}
public
void
setapiname(string apiname) {
this
.apiname = apiname;
}
public
string getapidescription() {
return
this
.apidescription;
}
public
void
setapidescription(string apidescription) {
this
.apidescription = apidescription;
}
public
integer getvalid() {
return
this
.valid;
}
public
void
setvalid(integer valid) {
this
.valid = valid;
}
public
date getupdatetime() {
return
this
.updatetime;
}
public
void
setupdatetime(date updatetime) {
this
.updatetime = updatetime;
}
public
date getcreatetime() {
return
this
.createtime;
}
public
void
setcreatetime(date createtime) {
this
.createtime = createtime;
}
public
string gethandlerclass() {
return
handlerclass;
}
public
void
sethandlerclass(string handlerclass) {
this
.handlerclass = handlerclass;
}
}
|
参数类信息 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
public
class
apiparam {
/**
*
*/
private
static
final
long
serialversionuid = 1l;
/**
* api表主键id
*/
private
long
id;
/**
*
* api名称
*/
private
string apiname;
/**
*
* 有效or无效
*/
private
integer valid;
public
string getapiname() {
return
apiname;
}
public
void
setapiname(string apiname) {
this
.apiname = apiname;
}
public
integer getvalid() {
return
valid;
}
public
void
setvalid(integer valid) {
this
.valid = valid;
}
public
long
getid() {
return
id;
}
public
void
setid(
long
id) {
this
.id = id;
}
/*@override
public string tostring() {
return "apiparam [id=" + id + ", apiname=" + apiname + ", valid=" + valid + "]";
}
*/
}
|
总结 。
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对我的支持.
原文链接:http://blog.51cto.com/shangdc/2153324 。
最后此篇关于Spring AOP如何整合redis(注解方式)实现缓存统一管理详解的文章就讲到这里了,如果你想了解更多关于Spring AOP如何整合redis(注解方式)实现缓存统一管理详解的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
我会尽可能地解释我正在做的事情,以获得最好的可能的建议/解决方案。这一切都是在 java 中完成的。 我的客户有一个基于 SWING 的桌面应用程序,它将使用 WebStart 加载。我被指派为用户帐
看来这个page包含 Azure CLI 支持的与 Azure API 管理相关的所有功能。但它没有展示如何使用 Azure CLI 管理用户、产品、证书、订阅和 API 等实体。 Azure CLI
我设置了一个 Hadoop 1.2.x 版本,双节点集群。第一节点(NameNode、Jobtracker)和第二节点(Secondary NameNode、Datanode、TaskTracker)
对于内容驱动的网站,设计好坏的关键是关系型数据库。在这个教程中,我们已经使用了MySQL关系型数据库管理系统(RDBMS)建立了我们的数据库。对于网站的开发者来说,MySQL是一个较受欢迎的选择,这
在尝试运行MariaDB之前,首先确定其当前状态,运行或关闭。 有三个选项用于启动和停止MariaDB – 运行mysqld(MariaDB脚本)。 运行mysqld_safe启动脚本。
我在管理界面中遇到 StackedInlines 前缀的问题。我会尝试发布所有必要的代码。 models.py(简要) ##### Base classes class BaseItem(models
我是新来的。到目前为止,我一直在使用 MVC 模型并使用基本的 session 管理模型,即在 session 中存储一个 token 并检查每个请求。 我正在尝试对lift做同样的事情,但我的 se
我在 win 服务中使用 NHiberante。有时我得到 System.ObjectDisposedException: Session is closed! Object name: 'ISess
我正在尝试使用 HtmlUnit 登录 Facebook 页面并查看其 HTML 内容。我正在尝试通过 HtmlUnit 填写登录凭据,但在单击提交按钮时我没有看到正在执行的 session 。 在
我正在为一个相当大的项目开发一个带有 reactjs 的前端,该项目有两个主要接口(interface)。主站点的前端和管理员的前端。 我应该将它们开发为两个不同的项目还是 reactjs 中的一个项
短版 我有一个使用插件基础结构的应用程序。插件具有可配置的属性,可帮助它们了解如何完成工作。插件按配置文件分组以定义如何完成任务,配置文件存储在由 DataContractSerializer 序列化
如何管理 iPhone 应用程序中的用户 session ?我在应用程序的第一页上从用户那里获取了用户名和密码。用户可以随时注销。如何像其他 Web 应用程序一样在 iPhone 应用程序中存储 se
我正在使用 Azure API 管理,其中包含第三方论坛 (Discourse) 的链接。 api管理提供的默认登录系统用于注册用户。我想知道是否可以对 api 管理和论坛使用单点登录,这样用户就不必
我正在使用 Wordpress 建立一个网站,并且我想利用它的 session 。但我没有找到任何插件,甚至文档。在我开始破解之前有什么建议或引用吗? 注意:我问的是 WP 是否以及如何使用标准 PH
我已阅读《Azure in Action》一书中的以下内容:“在 Windows Azure 中,状态服务器或进程外 session 状态提供程序,不支持” 谁能告诉我为什么不支持这个。他们在书中没有
我有一个内联表单集,我想排除一些模型对象在表单集中显示。 例如。模型 B 具有模型 A 的外键,因此它是 1:n(A 对象有许多 B 对象)关系。现在在 A 管理编辑页面上,我已经获得了 B 的内联。
我正在开发一个基于 session 的项目。我在想,与银行类似,我会创建一张支票并为用户提供阻止 session 超时的能力。 我正在考虑创建一个 setInterval 来检查需要身份验证的空白页面
我正在为一位拥有 Magento 商店的客户工作。里面塞满了产品,但这些产品的名称有点乱。他并没有坚持一种命名约定,而是多年来使用了不同的约定。因此,每当他使用“管理”->“管理产品”部分中的“名称”
我使用大约十几个 XSLT 文件来提供大量输出格式。目前,用户必须知道导出的文件格式的扩展名,例如RTF、HTML、TXT。 我还想使用参数来允许更多选项。如果我可以将元数据嵌入 XSL 文件本身,那
我已阅读《Azure in Action》一书中的以下内容:“在 Windows Azure 中,状态服务器或进程外 session 状态提供程序,不支持” 谁能告诉我为什么不支持这个。他们在书中没有
我是一名优秀的程序员,十分优秀!