hibernate redis 二級緩存

文章整理上傳於2017年9月12日,此處貼出參考過的文章:

http://wzalong.iteye.com/blog/2324290

https://github.com/debop/hibernate-redis

1.環境要求

如果想要直接運行效果,需要有一個hibernate項目和redis集羣

springmvc+hibernate的項目:https://git.oschina.net/ffiing/springmvc_hibernate_base_maven_webapp.git

win7已經配置好的redis集羣:https://git.oschina.net/ffiing/win_redis_cluster_zip.git

win-redis集羣教程:http://blog.csdn.net/qq_34545192/article/details/77945678


2.maven依賴

<dependency> 
<groupId>com.github.debop</groupId> 
 <artifactId>hibernate-redis</artifactId> 
 <version>2.3.2</version> 
 </dependency>

3.hibernate.xml配置修改:

<!--hibernate.xml其他配置依舊,  開啓二級緩存-->
<prop key="hibernate.cache.use_second_level_cache">${hibernate.cache.use_second_level_cache}</prop>
<prop key="hibernate.cache.use_query_cache">${hibernate.cache.use_query_cache}</prop>
<prop key="hibernate.cache.region.factory_class">${hibernate.cache.region.factory_class}</prop>
<prop key="hibernate.cache.provider_configuration_file_resource_path">
    ${hibernate.cache.provider_configuration_file_resource_path}
</prop>
<prop key="hibernate.cache.region_prefix">${hibernate.cache.region_prefix}</prop>

# hibernate-redis.properties
#redisson-config=classpath:redisson.yaml#單例
redisson-config=classpath:redisson.json#集羣
# Cache Expiry settings
# 'hibernate' is second cache prefix
# 'common', 'account' is actual region name
redis.expiryInSeconds.default=120
redis.expiryInSeconds.hibernate.common=0
redis.expiryInSeconds.hibernate.account=1200


4.redisson.json(集羣版)和redisson.yaml(單實例版)配置:(二選一,可在配置.properties中修改啓動)

redisson.json(集羣版):

{
  "clusterServersConfig": {
    "idleConnectionTimeout": 10000,
    "pingTimeout": 1000,
    "connectTimeout": 10000,
    "timeout": 3000,
    "retryAttempts": 3,
    "retryInterval": 5000,
    "reconnectionTimeout": 5000,
    "failedAttempts": 3,
    "password": "123456",
    "subscriptionsPerConnection": 5,
    "clientName": null,
    "loadBalancer": {
      "class": "org.redisson.connection.balancer.RoundRobinLoadBalancer"
    },
    "slaveSubscriptionConnectionMinimumIdleSize": 1,
    "slaveSubscriptionConnectionPoolSize": 50,
    "slaveConnectionMinimumIdleSize": 5,
    "slaveConnectionPoolSize": 64,
    "masterConnectionMinimumIdleSize": 5,
    "masterConnectionPoolSize": 64,
    "readMode": "MASTER_SLAVE",
    "nodeAddresses": [
      "redis://127.0.0.1:7001",
      "redis://127.0.0.1:7002",
      "redis://127.0.0.1:7003",
      "redis://127.0.0.1:7004",
      "redis://127.0.0.1:7005",
      "redis://127.0.0.1:7006"
    ],
    "scanInterval": 5000
  },
  "threads": 0,
  "codec": {
    "class": "org.redisson.codec.SnappyCodec"
  },
  "useLinuxNativeEpoll": false
}

redisson.yaml(單實例):

singleServerConfig:
  idleConnectionTimeout: 10000
  pingTimeout: 5000
  connectTimeout: 5000
  timeout: 5000
  retryAttempts: 1
  retryInterval: 1000
  reconnectionTimeout: 3000
  failedAttempts: 1
  password: 123456
  subscriptionsPerConnection: 3
  clientName: null
  address: [
   "redis://127.0.0.1:6379"
   ]
  subscriptionConnectionMinimumIdleSize: 1
  subscriptionConnectionPoolSize: 3
  connectionMinimumIdleSize: 3
  connectionPoolSize: 3
  database: 0
  dnsMonitoring: false
  dnsMonitoringInterval: 5000
threads: 0
# Codec
codec: !<org.redisson.codec.SnappyCodec> {}
useLinuxNativeEpoll: false
eventLoopGroup: null

5.結束,錯誤請百度微笑,具體配置說明請參考開頭兩篇文章

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