hibernate redis 二级缓存

来源:互联网 发布:知天下资源吗 编辑:程序博客网 时间:2024/06/05 20:58

文章整理上传于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 nameredis.expiryInSeconds.default=120redis.expiryInSeconds.hibernate.common=0redis.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: 5000threads: 0# Codeccodec: !<org.redisson.codec.SnappyCodec> {}useLinuxNativeEpoll: falseeventLoopGroup: null

5.结束,错误请百度微笑,具体配置说明请参考开头两篇文章

原创粉丝点击