问题描述
我正在使用在我的EF上下文中提供二级缓存。
我碰到了一个问题,我的一个实体连接到一个提供行级安全性的视图。因此,此视图会根据一些参数过滤行。当使用二级缓存时,所有用户将获得相同的结果!
我正在寻找一种方法来排除某些实体缓存,任何帮助都是受欢迎的。 / p>
这是我的缓存配置:
class CacheConfiguration:DbConfiguration
{
public CacheConfiguration()
{
var transactionHandler = new CacheTransactionHandler(new InMemoryCache());
AddInterceptor(transactionHandler);
var cachingpolicy = new cachingpolicy();
加载+ =
(sender,e)=> e.ReplaceService< DbProviderServices>(
(s,_)=> new CachingProviderServices(s,transactionHandler,cachingPolicy));
}
}
在。
为了排除一些实体,您需要创建一个缓存策略,并从缓存策略
。
在覆盖 CanBeCached
方法后,您可以返回 false
以防止缓存。
这是我的工作代码:
class CacheConfiguration:DbConfiguration
{
public CacheConfiguration()
{
var transactionHandler = new CacheTransactionHandler(new InMemoryCache());
AddInterceptor(transactionHandler);
// var cachingPolicy = new CachingPolicy();
var cachingPolicy = new myCachingPolicy();
加载+ =
(sender,e)=> e.ReplaceService< DbProviderServices>(
(s,_)=> new CachingProviderServices(s,transactionHandler,cachingPolicy));
}
}
public class myCachingPolicy:CachingPolicy
{
protected override bool CanBeCached(System.Collections.ObjectModel.ReadOnlyCollection< System.Data.Entity .Core.Metadata.Edm.EntitySetBase> affectedEntitySets,string sql,IEnumerable< KeyValuePair< string,object>>参数)
{
string [] excludedEntities = {
permView1,
permView2,
permView3};
if(affectedEntitySets.Where(x => excludedEntities.Contains(x.Table))。Any())
{
return false;
}
else
{
return base.CanBeCached(affectedEntitySets,sql,parameters);
}
}
}
I'm using EFCache to provide 2nd-level caching in my EF context.
I ran into a problem, where one of my entities is connected to a view which provides row-level security. So this view filters rows based on some parameters. When using 2nd-level cache, all users will get the same result!
I'm looking for a way to exclude certain entities from caching, any help is welcome.
This is my caching configuration:
class CacheConfiguration : DbConfiguration
{
public CacheConfiguration()
{
var transactionHandler = new CacheTransactionHandler(new InMemoryCache());
AddInterceptor(transactionHandler);
var cachingpolicy = new cachingpolicy();
Loaded +=
(sender, e) => e.ReplaceService<DbProviderServices>(
(s, _) => new CachingProviderServices(s, transactionHandler, cachingPolicy));
}
}
I found the answer in this blog post.
In order to exclude some entities, you need to create a caching policy and drive a class from CachingPolicy
.
After overriding CanBeCached
method, you can return false
to prevent caching.
This is my working code:
class CacheConfiguration : DbConfiguration
{
public CacheConfiguration()
{
var transactionHandler = new CacheTransactionHandler(new InMemoryCache());
AddInterceptor(transactionHandler);
//var cachingPolicy = new CachingPolicy();
var cachingPolicy = new myCachingPolicy();
Loaded +=
(sender, e) => e.ReplaceService<DbProviderServices>(
(s, _) => new CachingProviderServices(s, transactionHandler, cachingPolicy));
}
}
public class myCachingPolicy : CachingPolicy
{
protected override bool CanBeCached(System.Collections.ObjectModel.ReadOnlyCollection<System.Data.Entity.Core.Metadata.Edm.EntitySetBase> affectedEntitySets, string sql, IEnumerable<KeyValuePair<string, object>> parameters)
{
string[] excludedEntities = {
"permView1",
"permView2",
"permView3"};
if (affectedEntitySets.Where(x => excludedEntities.Contains(x.Table)).Any())
{
return false;
}
else
{
return base.CanBeCached(affectedEntitySets, sql, parameters);
}
}
}
这篇关于从二级缓存中排除某些实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!