if (context.Cache["Brand"]==null)
{
List<BrandInfo> brandinfoinfo = brand_bll.GetBrand(0, "");
context.Cache["Brand"] = brandinfoinfo;
context.Cache.Insert("Brand", brandinfoinfo, null, DateTime.Now.AddMinutes(5), TimeSpan.Zero);
}
info = context.Cache["Brand"] as List<BrandInfo>;
当count的值改变的时候,移除缓存!该怎么写呢?
public void Dependencies(HttpContext context)
{
int count = (context.Cache["Brand"] as List<BrandInfo>).Count;
int COUNT = brand_bll.GetBrand(0, "").Count;
if (COUNT > count)
{
//context.Cache["Brand"]
}
}
{
List<BrandInfo> brandinfoinfo = brand_bll.GetBrand(0, "");
context.Cache["Brand"] = brandinfoinfo;
context.Cache.Insert("Brand", brandinfoinfo, null, DateTime.Now.AddMinutes(5), TimeSpan.Zero);
}
info = context.Cache["Brand"] as List<BrandInfo>;
当count的值改变的时候,移除缓存!该怎么写呢?
public void Dependencies(HttpContext context)
{
int count = (context.Cache["Brand"] as List<BrandInfo>).Count;
int COUNT = brand_bll.GetBrand(0, "").Count;
if (COUNT > count)
{
//context.Cache["Brand"]
}
}
解决方案 »
- 动态生成导航栏里的项目
- 如何在B/S(asp.net)项目中通过一个按钮打开一个.exe文件。
- 求助:动态生成HTML列表问题!大家都来看看!谢谢!
- 我是做.Net程序开发,只知道天天做程序,也不知今年大家的事业爱情如何?
- 怎样设置前台没有绑定数据的gridview某一列宽度?
- 请问access的连接字符串如何写在web.config里?
- ★★★★★怎样将文本文件根据XML架构文件(XSD)生成XML文件?
- [求助]Form验证问题!
- js分条件显示内容,调试始终成功!
- 怎么获取所有在线用户名(session("username"))? 我想实现在线用户名统计
- VSS的引用问题
- 帮我看下这段代码哪里出错了?? gridview中点击编辑,再点击添加之后,gridview的编辑行和添加行都出来!如何取消编辑行????
key 用于引用该对象的缓存键。 value 要插入缓存中的对象。 dependencies 该项的文件依赖项或缓存键依赖项。当任何依赖项更改时,该对象即无效,并从缓存中移除。如果没有依赖项,则此参数包含空引用(Visual Basic 中为 Nothing)。 absoluteExpiration 所插入对象将过期并被从缓存中移除的时间。 slidingExpiration 最后一次访问所插入对象时与该对象过期时之间的时间间隔。如果该值等效于 20 分钟,则对象在最后一次被访问 20 分钟之后将过期并被从缓存中移除。 priority 该对象相对于缓存中存储的其他项的成本,由 CacheItemPriority 枚举表示。该值由缓存在退出对象时使用;具有较低成本的对象在具有较高成本的对象之前被从缓存移除。 onRemoveCallback 在从缓存中移除对象时将调用的委托(如果提供)。当从缓存中删除应用程序的对象时,可使用它来通知应用程序。
发现大多都是一样的!期待大侠帮帮忙!
//缓存用户数据,依赖于Cache["id"]
Cache["id"] = dr["id"].ToString();
CacheDependency cde = new CacheDependency(null, new string[] {"id"});
Cache.Insert("UserType", CurrentUserType, cde, ExpireyDate, Cache.NoSlidingExpiration);/// <summary>
/// 清空所有Cache
/// </summary>
protected void ClearCache()
{
foreach (DictionaryEntry cache in Cache)
{
Cache.Remove(cache.Key.ToString());
}
}也可以protected void Page_Load(object sender, EventArgs e)
{
//建立回调委托的一个实例
CacheItemRemovedCallback callBack = new CacheItemRemovedCallback(onRemove);
}private void onRemove(string key, object val, CacheItemRemovedReason reason)
{
//建立Cache回调委托的一个实例
CacheItemRemovedCallback callBack = new CacheItemRemovedCallback(onRemove);
Cache.Insert(key, val.ToString() + "*", null, System.DateTime.Now.AddHours(1), Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.Default, callBack);
}
CacheDependency cde = new CacheDependency(null, new string[] { "BrandCount" });
if (context.Cache["Brand"] == null)
{
List<BrandInfo> brandinfoinfo = brand_bll.GetBrand(0, "");
context.Cache["Brand"] = brandinfoinfo;
context.Cache.Insert("Brand", brandinfoinfo, cde, DateTime.Now.AddMinutes(5), Cache.NoSlidingExpiration);
}
info = context.Cache["Brand"] as List<BrandInfo>;
为什么我设置了缓存依赖项!每次content.Cache["Brand"]总是为空?
断点调试了下,每次都有执行if(content.Cache["Brand"]==null)括号里面?
怎么回事呢?
而且我数据没改!
object x=Cache["a"];
if(x==null)
{
depend =new CacheDependency("");
Cache.Insert("a", x, depnd, DateTime.Now.AddSeconds(5), TimeSpan.Zero, CacheItemPriority.Default, null);
}
我现在是想要当数据库有新增内容的时候,那么刚才的缓存如果没过期,这样就读不到最新的数据了,
然后设置了缓存依赖项,可是设置后Cache["Brand"]却总是为空了?哪里写错了呢!
重新贴下我改的代码
if (!string.IsNullOrEmpty(context.Request.Form["Letter"].ToString()))
{
context.Cache["BrandCount"] = (brand_bll.GetBrand(0, "") as List<BrandInfo>).Count;
string[] depencise = { "BrandCount" };
CacheDependency cde = new CacheDependency(null, depencise);
if (context.Cache["Brand"] == null)
{
List<BrandInfo> brandinfoinfo = brand_bll.GetBrand(0, "");
context.Cache["Brand"] = brandinfoinfo;
context.Cache.Insert("Brand", brandinfoinfo, cde, DateTime.Now.AddMinutes(5), Cache.NoSlidingExpiration);
}
info = context.Cache["Brand"] as List<BrandInfo>;
string name = context.Request.Form["Letter"].Trim();
if (info.Count > 0)
{
for (int i = 0; i < info.Count; i++)
{
string bname = info[i].BName;
string abc = Tools.Tools.GetPYString(bname.Substring(0, 1)).ToUpper();
if (abc == name)
{
sb.Append("{BName:'" + bname + "',ID:'" + info[i].ID + "',Logo:'" + info[i].Logo + "'}");
sb.Append("%");
}
}
}
if (string.IsNullOrEmpty(sb.ToString()))
sb.Append("false");
context.Response.Write(sb.ToString());
context.Response.End();
}
希望大家帮帮忙 谢谢了!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Data;
using System.Web.Caching; public static class DataBaseCache
{
public static void Add<T>( T o , string key , SqlCacheDependency denpendency )
{
HttpRuntime.Cache.Insert(
key ,
o ,
denpendency ,
System.Web.Caching.Cache.NoAbsoluteExpiration ,
TimeSpan.Zero );
} public static void Clear( string key )
{
HttpRuntime.Cache.Remove( key );
} public static bool Exists( string key )
{
return HttpRuntime.Cache[ key ] != null;
} public static bool Get<T>( string key , out T value )
{
try
{
if ( !Exists( key ) )
{
value = default( T );
return false;
} value = ( T ) HttpRuntime.Cache[ key ];
}
catch
{
value = default( T );
return false;
} return true;
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; using XUtils.Core.Configuration;
using System.Web.Caching;
using XUtils.Core.Data.Base;
using System.Data.SqlClient; /// <summary>
/// SQL缓存依赖类
/// </summary>
public static class SqlDependencyFactory
{
/// <summary>
/// 启动监听
/// </summary>
public static void Start()
{
SqlDependency.Start( Config.GetConnectionString( "ConnectionString" ) );
}
/// <summary>
/// 启动监听
/// </summary>
/// <param name="connectionString"></param>
public static void Start( string connectionString )
{
SqlDependency.Start( connectionString );
}
/// <summary>
/// 停止监听
/// </summary>
public static void Stop()
{
SqlDependency.Stop( Config.GetConnectionString( "ConnectionString" ) );
}
/// <summary>
/// 停止监听
/// </summary>
/// <param name="connectionString"></param>
public static void Stop( string connectionString )
{
SqlDependency.Stop( connectionString );
}
/// <summary>
/// 添加依赖对象
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key"></param>
/// <param name="t"></param>
public static void Add<T>( string key , T t )
{
if ( SqlDependencyFactory.isDependency )
{
Guard.IsNotNull( Database , "数据库名不能为空" );
Guard.IsNotNull( TableName , "表名不能为空" ); SqlCacheDependency dependency = new SqlCacheDependency( Database , TableName );
if ( !DataBaseCache.Exists( key ) )
{
DataBaseCache.Add<T>( t , key , dependency );
}
}
}
/// <summary>
/// 清除依赖对象
/// </summary>
public static void Clear( string key )
{
DataBaseCache.Clear( key );
}
/// <summary>
/// 检测依赖对象是否存在
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public static bool Exists( string key )
{
return DataBaseCache.Exists( key );
}
/// <summary>
/// 获取依赖缓存对象
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key"></param>
/// <returns></returns>
public static T Get<T>( string key )
{
T t = default( T );
if ( DataBaseCache.Exists( key ) )
{
DataBaseCache.Get<T>( key , out t );
}
return t;
} /// <summary>
/// 是否启动缓存依赖
/// </summary>
public static bool isDependency
{
get;
set;
}
/// <summary>
/// 数据库名
/// </summary>
public static string Database
{
get;
set;
}
/// <summary>
/// 表名
/// </summary>
public static string TableName
{
get;
set;
}
}
//////// 全局设置 /////////////
//启动缓存依赖
SqlDependencyFactory.Start();
//是否进行缓存依赖
SqlDependencyFactory.isDependency = true;
//缓存依赖数据库
SqlDependencyFactory.Database = "数据库名";//方法内部读取//配置依赖的数据表名
SqlDependencyFactory.TableName = "Sys_ServerConfig";
if ( SqlDependencyFactory.isDependency )
{
//缓存依赖
string key = "Sys_ServerConfig";
//检查缓存是否存在
if ( !SqlDependencyFactory.Exists( key ) )
{
//不存在则读取表数据
ReadServerConfig( entities , strSql.ToString() );
//加入缓存依赖
SqlDependencyFactory.Add<IList<ConfigurationServerEntity>>( key , entities );
}
else
//获取缓存数据
entities = SqlDependencyFactory.Get<IList<ConfigurationServerEntity>>( key );
}
else
//不启动表数据缓存依赖 直接读取数据
ReadServerConfig( entities , strSql.ToString() );
为数据库启动broker
ALTER DATABASE base2008 SET ENABLE_BROKER
查询broker 1为启动 0为未启动
Select DATABASEpRoPERTYEX('base2008','IsBrokerEnabled')
命令工具启动数据库的SQL缓存依赖支持使用SQL Server高速缓存依赖性的步骤:
一,使数据库支持SQL高速缓存依赖性。
二,使表支持SQL高速缓存依赖性。
三,在ASP.NET应用程序的web.config文件中包含SQL连接字符串。
四,以如下方式利用SQL高速缓存依赖性:
1)在代码中编程创建一个SqlCacheDependency对象。
2)给OutputCache指令添加SqlCacheDependency属性。
3)通过Response.AddCacheDependency给Response对象添加一SqlCacheDependency实例。开启高速缓存依赖性的参数简要说明
-d 用于SQL高速缓存依赖性的数据库名。数据库可以使用连接字符串和-c选项指定(必选)
-ed 允许数据库启用SQL高速缓存依赖性。
-dd 禁止数据库启用SQL高速缓存依赖性。
-et 允许表启用SQL高速缓存依赖性。需要-t选项。
-dt 禁止表启用SQL高速缓存依赖性。需要 -t 选项。
-t 支持或禁止SQL高速缓存依赖性的表名。需要 -et 或 -dt 选项。
-lt 列出所有启用SQL高速缓存依赖性的表。具体执行命令步骤如下:(1)开启数据库的Broker。(2)aspnet_regsql -S localhost -U sa -P sa -d Northwind -ed注:将本机上的Northwind数据库开启高速缓存依赖功能。(3)aspnet_regsql -S localhost -U sa -P sa -d Northwind -dd
注:禁用本机上的Northwind数据库的高速缓存依赖功能。使用次命令后,数据库中的所有表的高速缓存禁用功能都自动关闭,AspNet_SqlCacheTablesForChangeNotification表也会自动删除。(4)aspnet_regsql -S localhost -U sa -P sa -d Northwind -t Products -et
注:为Northwind数据库中的Products表开启高速缓存依赖功能。(5)aspnet_regsql -S localhost -U sa -P sa -d Northwind -t Products -dt
注:为 Northwind数据库中的Products表关闭高速缓存依赖功能。通过这个方法可以为一个数据库中的一个或多个表开启高速缓存依赖功能。为数据库和 表开启高速缓存依赖功能后,可以发现Northwind数据库中添加了一个 AspNet_SqlCacheTablesForChangeNotification表,表示配置成功了。(6)aspnet_regsql -S localhost -U sa -P sa -d Northwind -lt
使用-lt参数可以查看该数据库中哪些表开启了高速缓存依赖功能。如果数据库关闭了高速缓存依赖或者没有启用高速缓存依赖功能,使用此命令将会得到错误提示。 说明:我们在创建数据库的缓存依赖时,需要执行步骤(1)、(2)和(4),当要取消时,执行步骤(3)和(5)。
config配置<system.web>
<caching>
<!-- 设置没5秒去拉一下SQL数据库的修改信息-->
<sqlCacheDependency pollTime="5000">
<databases>
<add name="base2008" connectionStringName="ConnectionString"/>
</databases>
</sqlCacheDependency>
</caching>
</system.web>