mybatis整合mencached

来源:互联网 发布:java读取xml配置文件 编辑:程序博客网 时间:2024/06/08 02:23
官方放出了mybatis和memcached的整合包,先附上官方文档地址
http://mybatis.github.io/memcached-cache/
文档很简洁,事实证明使用起来也很简单
memcached的安装我这里就不再讲了,网上很容易找到
在项目中引入
<dependency>    <groupId>org.mybatis.caches</groupId>    <artifactId>mybatis-memcached</artifactId>    <version>1.0.0</version>  </dependency>
然后在想使用的mapper中加入
<mapper namespace="org.acme.FooMapper">  <cache type="org.mybatis.caches.memcached.MemcachedCache" />  ...</mapper>
就可以用了

再建一个memcached.properties,对他进行配置
PropertyDefaultDescriptionorg.mybatis.caches.memcached.keyprefix_mybatis_any string identifierorg.mybatis.caches.memcached.serverslocalhost:11211space separated list of host:{port}org.mybatis.caches.memcached.connectionfactorynet.spy.memcached.DefaultConnectionFactoryAny class that implementsnet.spy.memcached.ConnectionFactoryorg.mybatis.caches.memcached.expirationthe number of seconds in 30 daysthe expiration time (in seconds)org.mybatis.caches.memcached.asyncgetfalseflag to enable/disable the async getorg.mybatis.caches.memcached.timeout5the timeout when using async getorg.mybatis.caches.memcached.timeoutunitjava.util.concurrent.TimeUnit.SECONDSthe timeout unit when using async getorg.mybatis.caches.memcached.compressionfalseif true, objects will be GZIP compressed before putting them to Memcached我简单测试了一下发现它可以配置多个服务器,用逗号分隔,经测试如果某一台挂掉,他会选择正常的那台
如果2台都挂掉,就会报错,估计我们还是希望在memcached服务器挂掉后从数据库读取数据,不知道大家有什么好的实现方式或者思路吗
org.mybatis.caches.memcached.servers=172.29.33.201:11211,localhost:11211org.mybatis.caches.memcached.expiration=30 org.mybatis.caches.memcached.asyncget=false 

0 0
原创粉丝点击