JFinal 框架中緩存的使用

1.j2cache緩存的使用
1.1 一級緩存使用Redis;二級緩存使用Ehcache
二級緩存Ehcache是內存緩存;二級緩存是爲了緩解一級緩存的壓力;
設計原理:
https://img-blog.csdn.net/20180827132737551?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM0MTY5ODAy/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70
讀取數據:
https://img-blog.csdn.net/20180827132817303?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM0MTY5ODAy/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70

更新數據:
https://img-blog.csdn.net/20180827132856492?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM0MTY5ODAy/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70

1.2 緩存的配置:ehcache.xml
以下是參數說明:
name - Sets the name of the cache. This is used to identify the cache. It must be unique.
maxInMemory - Sets the maximum number of objects that will be created in memory
eternal - Sets whether elements are eternal. If eternal, timeouts are ignored and the element
is never expired.
timeToIdleSeconds - Sets the time to idle for an element before it expires. Is only used
if the element is not eternal. Idle time is now - last accessed time
timeToLiveSeconds - Sets the time to live for an element before it expires. Is only used
if the element is not eternal. TTL is now - creation time
overflowToDisk - Sets whether elements can overflow to disk when the in-memory cache
has reached the maxInMemory limit.

 <defaultCache
          // 這裏是默認緩存配置
    </defaultCache>

<ehcache updateCheck="false" dynamicConfig="false"  >
<cache name="session"
           maxElementsInMemory=
           eternal=
           timeToIdleSeconds=
           timeToLiveSeconds=
           overflowToDisk= />

</ehcache>

2.j2cache.properties 配置文件


# Cache Broadcast Method
# values:
# jgroups -> use jgroups's multicast
# redis -> use redis publish/subscribe mechanism

cache.broadcast=redis


# Level 1&2 provider
# values:
# none -> disable this level cache
# ehcache -> use ehcache as level 1 cache
# redis -> use redis as level 2 cache
# [classname] -> use custom provider

cache.L1.provider_class=ehcache
cache.L2.provider_class=redis

# Cache Serialization Provider
# values:
# fst -> fast-serialization
# java -> java standard
# [classname implements Serializer]

cache.serialization = fst

# Redis connection configuration

## connection
redis.host = localhost
redis.port = 6379
redis.timeout = 2000
redis.password = 123456
redis.database = 1

## properties
redis.maxActive = 100
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

3.j2cache的使用J2CacheKit 類中包括set get remove 等方法
登陸時可以緩存用戶 校驗碼的緩存等

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章