官方放出了mybatis和memcached的整合包,先附上官方文档地址
我简单测试了一下发现它可以配置多个服务器,用逗号分隔,经测试如果某一台挂掉,他会选择正常的那台
文档很简洁,事实证明使用起来也很简单
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,对他进行配置
Property | Default | Description |
---|---|---|
org.mybatis.caches.memcached.keyprefix | _mybatis_ | any string identifier |
org.mybatis.caches.memcached.servers | localhost:11211 | space separated list of host: {port} |
org.mybatis.caches.memcached.connectionfactory | net.spy.memcached.DefaultConnectionFactory | Any class that implementsnet.spy.memcached.ConnectionFactory |
org.mybatis.caches.memcached.expiration | the number of seconds in 30 days | the expiration time (in seconds) |
org.mybatis.caches.memcached.asyncget | false | flag to enable/disable the async get |
org.mybatis.caches.memcached.timeout | 5 | the timeout when using async get |
org.mybatis.caches.memcached.timeoutunit | java.util.concurrent.TimeUnit.SECONDS | the timeout unit when using async get |
org.mybatis.caches.memcached.compression | false | if true, objects will be GZIP compressed before putting them to Memcached |
如果2台都挂掉,就会报错,估计我们还是希望在memcached服务器挂掉后从数据库读取数据,不知道大家有什么好的实现方式或者思路吗
org.mybatis.caches.memcached.servers=172.29.33.201:11211,localhost:11211 org.mybatis.caches.memcached.expiration=30 org.mybatis.caches.memcached.asyncget=false