发表于: 2017-07-12 17:59:11

1 1418


今天:


Spring Cache:


阅读了《精通Spring 4.x》书中的Spring Cache一章的内容,对缓存,在Java中如何实现缓存,在Spring中如何实现缓存有了更深的理解。


Java对象只有实现了序列化接口,才能被JVM序列化。对象本身不用实现接口的任何方法。


缓存应该加在服务层。


缓存的基本逻辑是,先查缓存,缓存中没有就查数据库,再把查到的插入缓存中。


Spring本身提供默认的缓存管理注解。直接在服务层使用缓存注解,可以和缓存逻辑进行分离。


在Service方法上加入并标明缓存的名字,@Cacheable(cacheNames = “users” )。


只有public方法可以被缓存。


缓存的键,Spring有自带的SimpleKeyGenerator根据方法名和入参自动生成。

—————————————————————————————————————————————————————————


用了现在比较流行的Redisson。Redisson提供了将Redis无缝整合到Spring框架的能力。Redisson完整的实现了Spring框架里的缓存机制(Spring Cache Abstraction)。


Redisson有很多功能,这里只用它和Spring Cache整合的功能。其他以后有需要了再研究。


导入Redisson包:

<dependency>
    <groupId>org.redisson</groupId>
    <artifactId>redisson</artifactId>
    <version>3.4.3</version>
</dependency>


添加一个配置文件类,配置方法如下,没有配置的都使用默认(Redis如果不在本地,需要在Redis配置文件中把127.0.0.1注解掉,类似MySQL):

public class RedissonConfig {
    @Configuration
    @ComponentScan
    @EnableCaching
    public static class Application {
        @Bean(destroyMethod = "shutdown")
            RedissonClient redisson() throws IOException {
            Config config = new Config();
            config.useSingleServer().setAddress("redis://127.0.0.1:6379");
            return Redisson.create(config);
        }
        @Bean
            CacheManager cacheManager(RedissonClient redissonClient) {
            Map<String, CacheConfig> config = new HashMap<String, CacheConfig>();
            config.put("testMap", new CacheConfig(24 * 60 * 1000, 12 * 60 * 1000));
            return new RedissonSpringCacheManager(redissonClient, config);
        }
    }
}


直接在Service层把想要缓存返回值的方法上面加上@Cacheable即可,Spring Cache会自动使用Redisson进行缓存,自动实现缓存的逻辑(先查缓存,缓存没有查服务器,再插入缓存中):

@Cacheable(cacheNames = "users")
public List<User> getAllUsers(){
—————————————————————————————————————————————————————————


对自己两台服务器(一台金山一台阿里)分别进行不缓存和缓存的测试,数据库在金山。都为两线程。

金山无缓存:
Label
# Samples
Average
Median
90% Line
95% Line
99% Line
Min
Max
Error %
Throughput
Received KB/sec
Sent KB/sec
addUser
4822
90
46
262
331
589
20
2052
13.936%
22.04141
106.45
2.84
金山有缓存:
Label
# Samples
Average
Median
90% Line
95% Line
99% Line
Min
Max
Error %
Throughput
Received KB/sec
Sent KB/sec
addUser
5872
78
20
272
349
640
8
4037
0.034%
25.3522
109.39
3.27
阿里无缓存(因为数据库在金山,所以较差):
Label
# Samples
Average
Median
90% Line
95% Line
99% Line
Min
Max
Error %
Throughput
Received KB/sec
Sent KB/sec
addUser-aliyun
4116
106
81
149
229
499
55
3611
18.829%
18.67115
84.33
2.41
阿里有缓存(性能好于金山,虽然都是一核2G,但是阿里的主频较高):
Label
# Samples
Average
Median
90% Line
95% Line
99% Line
Min
Max
Error %
Throughput
Received KB/sec
Sent KB/sec
addUser-aliyun
8858
72
42
126
203
436
19
11207
0.023%
27.69735
119.52
3.57
结果:在默认配置下,使用Redis缓存就已经可以减少错误率,增加吞吐量,大幅度减少对数据库的访问量,但如果缓存时间过长,也会带来返回的值和数据库不同步的问题。阿里服务器好于金山服务器。
—————————————————————————————————————————————————————————


在服务器端安装并打开redis-server时,可能会有三个WARNING,用以下命令可以消掉这些警告:


echo 511 > /proc/sys/net/core/somaxconn
sysctl -w vm.overcommit_memory=1

echo never > /sys/kernel/mm/transparent_hugepage/enabled


可以在配置文件中把进程设置为yes,Redis就可以在后台运行了(默认是no)。


明天:开始任务7


问题:无


总结:无



返回列表 返回列表
评论

    分享到