spring+memcache缓存注解配置

spring+memcache配置缓存注解@cahceable等

1.增加memcache pom文件

1
2
3
4
5
<dependency>
<groupId>com.googlecode.xmemcached</groupId>
<artifactId>xmemcached</artifactId>
<version>2.0.0</version>
</dependency>

1.配置memcache xml

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
<!-- xmemcached 配置 -->
<bean name="memcachedClient"
class="net.rubyeye.xmemcached.utils.XMemcachedClientFactoryBean"
destroy-method="shutdown">
<property name="servers">
<!-- ip地址 端口号-->
<value>127.0.0.1:11211</value>
</property>
<!-- server's weights -->
<property name="weights">
<list>
<value>1</value>
</list>
</property>
<!-- nio connection pool size -->
<property name="connectionPoolSize" value="2"></property>
<!-- Use binary protocol,default is TextCommandFactory -->
<property name="commandFactory">
<bean class="net.rubyeye.xmemcached.command.BinaryCommandFactory"></bean>
</property>
<!-- Distributed strategy -->
<property name="sessionLocator">
<bean class="net.rubyeye.xmemcached.impl.KetamaMemcachedSessionLocator"></bean>
</property>
<!-- Serializing transcoder -->
<property name="transcoder">
<bean class="net.rubyeye.xmemcached.transcoders.SerializingTranscoder" />
</property>
<!-- ByteBuffer allocator -->
<property name="bufferAllocator">
<bean class="net.rubyeye.xmemcached.buffer.SimpleBufferAllocator"></bean>
</property>
<!-- Failure mode -->
<property name="failureMode" value="false" />
</bean>

3.增加cache 命名空间

1
2
3
4
5
6
7
8
<beans xmlns="http://www.springframework.org/schema/beans">
xmlns:cache="http://www.springframework.org/schema/cache"

xsi:schemaLocation="http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache-3.1.xsd"
>
//省略...
</beans>

4.编写MemcachedCache管理类

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
package com.xxx.cache;

import java.util.concurrent.TimeoutException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.Cache;
import org.springframework.cache.support.SimpleValueWrapper;



import net.rubyeye.xmemcached.MemcachedClient;
import net.rubyeye.xmemcached.exception.MemcachedException;


public class MemcachedCache implements Cache {

//这里要注入该项目的缓存
@Autowired
private MemcachedClient memcachedClient;

private String name;

public MemcachedCache() {

}

public MemcachedCache(String name, MemcachedClient memcachedClient) {
this.memcachedClient = memcachedClient;
this.name = name;
}

@Override
public String getName() {
return this.name;
}

@Override
public Object getNativeCache() {
return this.memcachedClient;
}

@Override
public ValueWrapper get( Object key ) {
Object value = null;
try {
value = this.memcachedClient.get( objectToString( key ) );
}
catch( TimeoutException | InterruptedException | MemcachedException e ) {

e.printStackTrace();
}
return (value != null ? new SimpleValueWrapper( value ) : null);
}

@Override
public void put( Object key, Object value ) {
try {
this.memcachedClient.set( objectToString( key ), 3600, value );
}
catch( TimeoutException | InterruptedException | MemcachedException e ) {

e.printStackTrace();
}

}

@Override
public void evict( Object key ) {
try {
this.memcachedClient.delete( objectToString( key ) );
}
catch( TimeoutException | InterruptedException | MemcachedException e ) {

e.printStackTrace();
}

}

@Override
public void clear() {

}

private static String objectToString( Object object ) {
if( object == null ) {
return null;
}
else if( object instanceof String ) {
return (String)object;
}
else {
return object.toString();
}
}



public void setName( String name ) {
this.name = name;
}

}

5.配置CacheManager

1
2
3
4
5
6
7
8
9
10
<!-- 启动缓存注解功能 -->
<cache:annotation-driven/>

<bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager">
<property name="caches">
<set>
<bean class="com.xxx.cache.MemcachedCache" p:name="xxxx"/>
</set>
</property>
</bean>

6.启用注解

1
2
3
4
5
6
@Cacheable( value = "xxxx", key = "#name" )
public String getName( String name ) {
System.err.println( "执行该方法:" + new Date() );
//do something
return name;
}
文章目录
  1. 1. spring+memcache配置缓存注解@cahceable等
    1. 1.0.1. 1.增加memcache pom文件
  2. 1.1. 1.配置memcache xml
  3. 1.2. 3.增加cache 命名空间
  4. 1.3. 4.编写MemcachedCache管理类
  5. 1.4. 5.配置CacheManager
    1. 1.4.1. 6.启用注解
|