---
title: "关于二级缓存"
author: "samana"
date: "2010-01-29T15:13:35Z"
tags: []
categories: ["企业架构"]
slug: "untitled"
draft: false
lastmod: "2010-01-29T15:13:35Z"
id: 583383
url: "https://samana.iteye.com/blog/583383"
---

对于cache来说，一般都是为了减少IO的读写频率，这就是其和buffer的区别。对于hibernate的二级缓存的概念，一直有一个不太明了清晰的概念，写些文字总结下。

     众所周知，hibernate提供了两级缓存，一级缓存是必须的，二级缓存是可选的（也可以认为是可插拔的）。    hibernate的session提供了一级缓存，每个session，对同一个id进行两次load，不会发送两条sql给数据库，但是session关闭的时候，一级缓存就失效了。 二级缓存是SessionFactory级别的全局缓存，它底下可以使用不同的缓存类库，比如ehcache、oscache等，需要设置hibernate.cache.provider\_class。原来PIM项目曾经用过 ehcache，将其作为session的缓存，而hibernate亦推荐ehcache作为其二级缓存的首选。

      但是ehcache还没有支持集群服务，所有网络上又有了利用memcached作为hibernate的二级缓存，以支持集群情况下的数据缓存，而这块应用在google code上也有了开源应用，链接如下： http://code.google.com/p/hibernate-memcached/，这个项目里的MemCached Client 应用的 danga,从更优的角度倒可以换成rubyeye的MemcachedClient.