初步认识 j2cache

今天聊聊大数据的二级缓存问题,
question
当浏览一个网页的时候,总会出现一些热门推荐的问题。若是直接查找数据库,若是像京东,天猫之类的访问流量按T算的网站,服务器的压力会很大。因此,把经常访问的内容缓存起来,提升网站的性能,减少服务器的压力。

当然,既然是说大数据,就要用到大数据的框架来解决--redis。

《初步认识 j2cache》

redis是基于encach一级缓存进行的二级缓存框架。所有的ehcach将订阅redis的功能,ehcache通过jedis代码向redis频道中发送数据,当redis的订阅频道检测到从ehcach发送过来的数据时,redis将发布收到的数据给所有订阅该频道的ehcach服务器,此时所有要缓存的数据都同步缓存到了ehcach框架中。

接下来研究具体的一个redis框架的源码--j2cache
j2cache的源码下载地址

<defaultCache
        maxElementsInMemory="1000"
        eternal="false"
        timeToIdleSeconds="60"
        timeToLiveSeconds="120"
        overflowToDisk="true">
        <!--  cacheEventListenerFactory class="net.oschina.j2cache.ehcache.EhCacheEventListenerFactory"/-->
    </defaultCache>
<cache name="session"
        maxElementsInMemory="5000"
        eternal="false"
        timeToIdleSeconds="1800"
        timeToLiveSeconds="1800"
        overflowToDisk="false"
        />

在这个资源文件中配置了j2cache二级缓存的大小maxElementsInMemory,淘汰闲置缓存的时间timeToIdleSeconds,内存溢出之后是否存储到磁盘overflowToDisk,

此时,我不是很明白timeToIdleSeconds timeToLiveSeconds ,通过查看文档明白:

1.TTI timeToIdleSeconds is the maximum number of seconds that an element can exist in the cache without being accessed:
TTI用于设置对象在cache中的最大闲置时间,就是 在一直不访问这个对象的前提下,这个对象可以在cache中的存活时间。
2.TTL timeToLiveSeconds is the maximum number of seconds that an element can exist in the cache whether
or not is has been accessed.
TTL用于设置对象在cache中的最大存活时间,就是 无论对象访问或是不访问(闲置),这个对象在cache中的存活时间。
3.If the eternal flag is set, elements are allowed to exist in the cache eternally and none are evicted。
当配置了 eternal ,那么TTI和TTL这两个配置将被覆盖,对象会永恒存在cache中,永远不会过期。

分析元数据J2Cache.properties

redis.host = localhost
redis.port = 6379
redis.timeout = 2000
# redis \u90E8\u7F72\u7B56\u7565 single \u5355\u673A; cluster \u96C6\u7FA4; sharded \u5206\u7247
redis.policy = single
##redis.password =
redis.database = 1

## redis cache namespace optional, default[j2cache]
redis.namespace = j2cache

## redis channel name, \u540C\u4E00\u4E2Aredis server\u5982\u679C\u6709\u591A\u4E2A\u4F7F\u7528J2Cache, \u8BF7\u4FDD\u6301channel name \u7684\u552F\u4E00\u6027
redis.channel_name = j2cache_channel

## properties
redis.maxTotal = -1
redis.maxIdle = 2000
redis.maxWaitMillis = 100
redis.minEvictableIdleTimeMillis = 864000000
redis.minIdle = 1000
redis.numTestsPerEvictionRun = 10
redis.lifo = false
redis.softMinEvictableIdleTimeMillis = 10
redis.testOnBorrow = true
redis.testOnReturn = false
redis.testWhileIdle = false
redis.timeBetweenEvictionRunsMillis = 300000
redis.blockWhenExhausted = true

主要在open stack中配置了redis服务器的地址,端口和其他的一些设置。

然后可以登录openstack启动一台redis服务器了,为redis服务器创建浮动ip方便scrt连接。
[root@localhost ~]# yum install redis

[root@localhost ~]# redis-server /etc/redis.conf 
[root@localhost ~]# netstat -natp
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name   
tcp        0      0 0.0.0.0:6379                0.0.0.0:*                   LISTEN      14555/redis-server  

中间有一个redis的配置需要做一个改动,否则不能启动redis服务器


[root@localhost ~]# vi /etc/redis.conf 

bind 0.0.0.0

浏览结果:

[root@localhost ~]# redis-cli 
redis 127.0.0.1:6379> set redis hello
OK
redis 127.0.0.1:6379> get redis
"hello"
redis 127.0.0.1:6379> 
    原文作者:shineDeveloper
    原文地址: https://www.jianshu.com/p/6b79932318d1
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞