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
NHibernate中使用memcache二级缓存的更多相关文章
- NHibernate使用MemCache二级缓存
首先,当然是安装MemCache服务器端了. 然后配置过程,仅仅两个问题. 1.NHibernate要与NHibernate.Cache的版本要一致.否则,NHibernate.Caches.MemC ...
- Hibernate JPA 中配置Ehcache二级缓存
在Hibernate3 JPA里配置了一下非分布式环境的二级缓存,效果不错.具体过程如下: 1, 需要引入的jar包 http://ehcache.org/downloads/catalog 下载的包 ...
- NHibernate系列文章十:NHibernate对象二级缓存下
摘要 上一节对NHibernate二级缓存做了简单介绍,NHibernate二级缓存是由SessionFactory管理的,所有Session共享.这一节介绍二级缓存其他两个方面:二级缓存查询和二级缓 ...
- [原创]关于mybatis中一级缓存和二级缓存的简单介绍
关于mybatis中一级缓存和二级缓存的简单介绍 mybatis的一级缓存: MyBatis会在表示会话的SqlSession对象中建立一个简单的缓存,将每次查询到的结果结果缓存起来,当下次查询的时候 ...
- SSM-MyBatis-18:Mybatis中二级缓存和第三方Ehcache配置
------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- 二级缓存 Mybatis中,默认二级缓存是开启的.可以关闭. 一级缓存开启的.可以被卸载吗?不可以的.一级缓存 ...
- 深入理解MyBatis中的一级缓存与二级缓存
http://blog.csdn.net/weixin_36380516/article/details/73194758 先说缓存,合理使用缓存是优化中最常见的,将从数据库中查询出来的数据放入缓 ...
- 关于mybatis中一级缓存和二级缓存的简单介绍
关于mybatis中一级缓存和二级缓存的简单介绍 mybatis的一级缓存: MyBatis会在表示会话的SqlSession对象中建立一个简单的缓存,将每次查询到的结果结果缓存起来,当下次查询的时候 ...
- 【MyBatis学习13】MyBatis中的二级缓存
1. 二级缓存的原理 前面介绍了,mybatis中的二级缓存是mapper级别的缓存,值得注意的是,不同的mapper都有一个二级缓存,也就是说,不同的mapper之间的二级缓存是互不影响的.为了更加 ...
- 缓存架构中的服务详解!SpringBoot中二级缓存服务的实现
创建缓存服务 创建缓存服务接口项目 创建myshop-service-redis-api项目,该项目只负责定义接口 创建项目的pom.xml: <?xml version="1.0&q ...
随机推荐
- ubuntu-14.04 系统安装mysql-5.6.21
1.安装mysql前准备工作 (1).从官网下载mysql-5.6.21.tar.gz (2).tar -zxvf mysql-5.6.21-tar.gz 会生成mysql-5.6.21的目录 ...
- 移动端 iphone touchmove滑到边界(浏览器地址拦及以上) touchend失效解决办法
在移动端h5页面:尤其那些全屏幕的盒展示切换页面,当用户无意中将手指滑到了 浏览器地址拦以上(中国移动这快区域):此时,手指已经离开屏幕了,但是ios上无法监听到touchend 事件:touchen ...
- JS列
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvU2h1aVRpYW5OYWlMdW8=/font/5a6L5L2T/fontsize/400/fill/I0 ...
- 怎么样sourceforge开源项目发现,centos安装-同htop安装案例
一个.htop什么? top是linux下经常使用的监控程序.htop相当于其加强版,颜色显示不同參数.且支持鼠标操作. 详细介绍參看此说明文档. watermark/2/text/aHR0cDovL ...
- SQL Server审计功能入门:更改跟踪(Change Tracking)
原文:SQL Server审计功能入门:更改跟踪(Change Tracking) 介绍 更改跟踪是一种轻量型解决方案,它为应用程序提供了一种有效的更改跟踪机制.常规的,自定义变更跟踪和读取跟踪数据, ...
- 杭州电 1203 I NEED A OFFER!
I NEED A OFFER! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- CoffeeScript NgComponent
Angular遇上CoffeeScript - NgComponent封装 CoffeeScript是基于JavaScript的一门扩展小巧语言,它需要编译成JavaScript,然后再运行与浏览器或 ...
- 【MongoDB】Serveral common command of MongoDb
In the recent days, since the overwork made me exhaused, on arrival to home I will go to bed, which ...
- Android发展简报
Android这个词的本义是指“机器人”.同时它是Google至2007年11月5根据公布Linux台的开源手机操作系统的名称,该平台由操作系统.中间件.用户界面和应用软件组成.号称是首个为移动终端打 ...
- Javascript学习4 - 对象和数组
原文:Javascript学习4 - 对象和数组 在Javascript中,对象和数组是两种基本的数据类型,而且它们也是最重要的两种数据类型. 对象是已命名的值的一个集合,而数组是一种特殊对象,它就像 ...