下面是在NHibernate中使用memcache二级缓存的详细操作记录。
一、Windows下安装Memcache
1、
下载 http://jehiah.cz/projects/memcached-win32/ memcached 1.2.1 for Win32 binaries ;
2、
解压 到 D:\memcached;
3、
安装 D:\memcached \memcached.exe -d install
4、
启动 D:\memcached \memcached.exe -d start
可以在进程中看到memcached.exe
5、
其他常用命令
-p 监听的端口
-l 连接的IP地址, 默认是本机
-d start 启动memcached服务
-d restart 重起memcached服务
-d stop|shutdown 关闭正在运行的memcached服务
-d install 安装memcached服务
-d uninstall 卸载memcached服务
-u 以的身份运行 (仅在以root运行的时候有效)
-m 最大内存使用,单位MB。默认64MB
-M 内存耗尽时返回错误,而不是删除项
-c 最大同时连接数,默认是1024
-f 块大小增长因子,默认是1.25
-n 最小分配空间,key+value+flags默认是48
-h 显示帮助
二、在NHibernate项目中配置Memcache
1、下载NHibernate第三方二级缓存提供程序 NHibernate.Caches.MemCache 。
http://sourceforge.net/projects/nhcontrib/files/ NHibernate.Caches/
2、在应用程序配置文件(app.config or web.config)中添加:
<
configSections
>
<
section
name
="memcache"
type
="NHibernate.Caches.MemCache.MemCacheSectionHandler,NHibernate.Caches.MemCache"
/>
</
configSections
>
<
memcache
>
<
memcached
host
="127.0.0.1"
port
="11211"
weight
="2"
/>
</
memcache
>
3、 在hibernate.cfg.xml中添加缓存相关配置。
1) 设置二级缓存提供程序
<
property
name
="cache.provider_class"
>
NHibernate.Caches.MemCache.MemCacheProvider,NHibernate.Caches.MemCache
</
property
>
2) 启用二级缓存
<
property
name
="cache.use_second_level_cache"
>
true
</
property
>
3) 启用查询缓存
<
property
name
="cache.use_query_cache"
>
true
</
property
>
4)设置过期时间(秒)
<
property
name
="cache.default_expiration"
>
300
</
property
>
5) 设置缓存的前缀名称
<
property
name
="cache.region_prefix"
>
Demo
</
property
>
6) 配置缓存实体
<
mapping
assembly
="Lee.Model"
/>
<
class-cache
class
="Lee.Model.UserInfo,Lee.Model"
usage
="read-write"
/>
hibernate.cfg.xml文件
<?
xml version="1.0" encoding="utf-8"
?>
<
hibernate-configuration
xmlns
='urn:nhibernate-configuration-2.2'
>
<
session-factory
>
<
property
name
="show_sql"
>
true
</
property
>
<
property
name
="dialect"
>
NHibernate.Dialect.MsSql2005Dialect
</
property
>
<
property
name
="connection.driver_class"
>
NHibernate.Driver.SqlClientDriver
</
property
>
<
property
name
="proxyfactory.factory_class"
>
NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle
</
property
>
<
property
name
="connection.connection_string_name"
>
SQLConnection
</
property
>
<
property
name
="cache.provider_class"
>
NHibernate.Caches.MemCache.MemCacheProvider,NHibernate.Caches.MemCache
</
property
>
<
property
name
="cache.use_second_level_cache"
>
true
</
property
>
<
property
name
="cache.use_query_cache"
>
true
</
property
>
<
property
name
="cache.default_expiration"
>
300
</
property
>
<
property
name
="cache.region_prefix"
>
Demo
</
property
>
<
mapping
assembly
="Lee.Model"
/>
<
class-cache
class
="Lee.Model.UserInfo,Lee.Model"
usage
="read-write"
/>
</
session-factory
>
</
hibernate-configuration
>
4、测试代码
请先下载以前用到的项目http://files.cnblogs.com/tenghoo/WCFDemo.rar,在项目中修改。
1)在Lee.DAL. UserInfoDAL中添加以下方法:
public
UserInfo getUserInfo()
{
UserInfo u
=
new
UserInfo();
using
(_session
=
_sessionfactory.Session)
{
u
=
_session.Get
<
UserInfo
>
(
1
);
}
return
u;
}
2)调用代码
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
Lee.Model;
using
Lee.DAL;
namespace
Lee.ConsoleTest
{
class
Program
{
static
void
Main(
string
[] args)
{
Console.WriteLine(
"
-第一次读-
"
);
UserInfoDAL dal
=
new
UserInfoDAL();
UserInfo u
=
dal.getUserInfo();
Console.WriteLine();
Console.WriteLine(
"
ID={0},Name={1}
"
, u.Id, u.Name);
Console.WriteLine(
"
-第二次读-
"
);
Console.WriteLine();
UserInfoDAL dal2
=
new
UserInfoDAL();
UserInfo u2
=
dal2.getUserInfo();
Console.WriteLine(
"
ID={0},Name={1}
"
, u2.Id, u2.Name);
}
}
}
5、开始测试
不启动memcache,启动项目
启动memcache ,启动项目
保持memcache启动状态,重启项目
三、扩展阅读
memcache服务器安全问题
http://www.soaspx.com/dotnet/service/service_20091113_1576.html