`
freetosoar
  • 浏览: 35237 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Linux下Memcache服务器端的安装及Memcache在Hibernate中应用

阅读更多

今天被接触到Memcache, 先把自己在网上找到的资料和自己实践的过程记录一起记录下来, 以供自己日后查阅.


Memcache服务器端的安装
    *安装libevent
    Memcache用到了libevent这个库用于Socket的处理,所以还需要安装libevent,libevent的最新版本是libevent-1.4.13。(如果你的系统已经安装了libevent,可以省去这一步骤)

   cd /tmp
   wget -c  http://monkey.org/~provos/libevent-1.4.13-stable.tar.gz
   tar zxvf libevent-1.4.13-stable.tar.gz
   cd libevent-1.4.13-stable
    #配置一个安装路径, 为/usr/local/libevent
   ./configure --prefix=/usr/local/libevent
   make && make install

  *安装memcached
   cd /tmp
   wget -c http://memcached.googlecode.com/files/memcached-1.4.5.tar.gz
   tar zxvf memcached-1.4.5.tar.gz
   cd memcached-1.4.5
   #配置memcached的安装目录, 并绑定到已经安装的libevent.
   ./configure --prefix=/usr/local/memcache --with-libevent=/usr/local/libevent
   make && make install
  *启动memcached

 

   ./memcached -d -m 10 -u root -p 11211  -P /tmp/memcached.pid


注意:
运行/usr/local/memcache/bin/memcached 时有类似以下错误提示
  /usr/local/memcache/bin/memcached : error while loading shared libraries: libevent-1.4.so.2: cannot open shared object file: No such file or directory

  #64位系统解决办法:
  ln -s /usr/local/libevent/lib/libevent-1.4.so.2 /usr/lib64/libevent-1.4.so.2
  #32位系统解决办法:
  ln -s /usr/local/libevent/lib/libevent-1.4.so.2 /usr/lib/libevent-1.4.so.2

在Hibernate中应用Memcached.
项目使用的是maven来管理, eclipse作为开发的工具. 下面的文字说明一这个开发环境做前提.

  *引入相关的jar到项目中 -- 在POM中新增如下的依赖

    <dependency>
        <groupId>com.googlecode</groupId>   
        <artifactId>hibernate-memcached</artifactId>
       <version>1.2</version>
   </dependency>

      如果你不能成功的下载相关的jar文件, 你应该新增下面的Repository到Pom

<repository>
   <id>hibernate-memcached</id>
   <name>hibernate-memcached</name>   
   <url>http://raykrueger.googlecode.com/svn/repository</url>
</repository>

    # 如果不是用maven来管理项目的jar文件依赖, 你可以按照下面的步骤导入所有需要的jar:
     1. 查看hibernate-memcached的pom文件, 打开该文件, 获取它的所有依赖的jar.
     2. 通过其他的方法下载所有被依赖的jar.
     3. 手动的导入所有的jars.

 

Hibernate中引用Memcacheed
   *修改Hibernate配置文件, 如下:
    #配置Hibernate使用cache提供类
     hibernate.cache.provider_class=com.googlecode.hibernate.memcached.MemcachedCacheProvider
     #设置查询缓存开启
     hibernate.cache.use_query_cache=true
    以上两个配置项是必须的(* ),
  # 其它一些可选配置参数设置说明:

 

    其它一些参数设置说明:

Property Default Description
hibernate.memcached.servers localhost:11211 memcached 服务地址,多个用空格分隔
格式host:port
hibernate.memcached.cacheTimeSeconds 300 缓存失效时间,单位秒
hibernate.memcached.keyStrategy HashCodeKeyStrategy 缓存Key生成存储HashCode算法
hibernate.memcached.readBufferSize

DefaultConnectionFactory.

DEFAULT_READ_BUFFER_SIZE

从服务器读取数据缓存区大小
hibernate.memcached.operationQueueLength

DefaultConnectionFactory.

DEFAULT_OP_QUEUE_LEN

Maximum length of the operation queue returned by this connection factory
hibernate.memcached.operationTimeout

DefaultConnectionFactory.

DEFAULT_OPERATION_TIMEOUT

操作超时时间设置
hibernate.memcached.hashAlgorithm HashAlgorithm.KETAMA_HASH 新增缓存数据到服务器时使用的Hash散列算法。 当 hibernate-memcached 设置成 KETAMA_HASH算法时,注意:默认客户端API使用的是 HashAlgorithm.NATIVE_HASH
hibernate.memcached.clearSupported false 支持MemcachedCache.clear()方法清空缓存。
建议不要开启。

 

  #在Hibernate的Model类中增加缓存策略, 可以用Annotation来实现 如下,
@Cache(usage=CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)

如果用到了One-to-many, 而且想缓存对应的Set, 可以在Set对应的Getter方法上加上上面的@Cache

CacheConcurrencyStrategy中一共有五个常量:
NONE – 不缓存
READ_ONLY,  只读模式,在此模式下,如果对数据进行更新操作,会有异常;
READ_WRITE,读写模式, 在更新缓存的时候会把缓存里面的数据换成一个锁
NONSTRICT_READ_WRITE,不严格的读写模式则不会的缓存数据加锁;
TRANSACTIONAL,事务模式指缓存支持事务,当事务回滚时,缓存也能回滚,只支持JTA环境

请根据各自Model的所起的具体作用选用适合的策略

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics