我已浏览此Microsoft文档Caching Support for WCF Web HTTP Services,并设法为用户列表创建缓存。
这是它的外观:
public List<ForJson.User> UserList()
{
bool nil;
WebOperationContext.Current.IncomingRequest.CheckConditionalRetrieve(this.idGenerator.GetId(this.usersEtag, out nil));
var userList = this.UnitOfWork.UserRepository.GetAll()
.Result.Select(u => new ForJson.User
{
FirstName = u.FirstName,
LastName = u.LastName,
UserId = u.UserId,
CardNumber = u.CardNumber
})
.ToList();
WebOperationContext.Current.OutgoingResponse.SetETag(this.idGenerator.GetId(this.usersEtag, out nil));
return userList;
}
我还希望为特定用户的资产列表实现此功能,但没有运气。我发现很难理解我做错了什么。如果有人能指出我的错误:
public List<ForJson.Asset> AssetList(int? userId)
{
bool nil;
List<ForJson.Asset> assetList;
if (userId != null)
{
if (!this.assetEtag.TryGetValue(userId.Value, out object eTag))
{
eTag = new object();
this.assetEtag.Add(userId.Value, eTag);
}
WebOperationContext.Current.IncomingRequest.CheckConditionalRetrieve(this.idGenerator.GetId(eTag, out nil));
assetList = this.UnitOfWork.UserRepository.Get(userId.Value)
.Result.Assets.Select(a => new ForJson.Asset
{
AssetId = a.AssetId,
Barcode = a.Barcode,
Type = a.Type.Name,
UserId = userId.Value
})
.ToList();
WebOperationContext.Current.OutgoingResponse.SetETag(this.idGenerator.GetId(eTag, out nil));
return assetList;
}
WebOperationContext.Current.IncomingRequest.CheckConditionalRetrieve(this.idGenerator.GetId(this.assetEtag, out nil));
assetList = this.UnitOfWork.AssetRepository.Find(a => a.UserId == null)
.Result.Select(a => new ForJson.Asset
{
AssetId = a.AssetId,
Barcode = a.Barcode,
Type = a.Type.Name
})
.ToList();
WebOperationContext.Current.OutgoingResponse.SetETag(this.idGenerator.GetId(this.assetEtag, out nil));
return assetList;
}
现在如何运作?就像它在下一个方法调用中不会将userId考虑在内。当第一次调用for userId然后接下来的5分钟时,它返回该用户的资产(无论用户是否为null或任何其他id)
<outputCacheSettings>
<outputCacheProfiles>
<add name="CacheFor5Minutes" duration="300" varyByParam="none" sqlDependency="AssetsSystem:Users;AssetsSystem:Assets"/>
</outputCacheProfiles>
</outputCacheSettings>
[OperationContract]
[WebGet(ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Bare,
UriTemplate = "userList")]
[AspNetCacheProfile("CacheFor5Minutes")]
List<ForJson.User> UserList();
[OperationContract]
[WebGet(ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Bare,
UriTemplate = "assetList?userId={userId}")]
[AspNetCacheProfile("CacheFor5Minutes")]
List<ForJson.Asset> AssetList(int? userId);
答案 0 :(得分:0)
灵魂就是在userId
veryByParam
添加为web.config
<add name="CacheFor5MinutesByUser" duration="300" varyByParam="userId" sqlDependency="AssetsSystem:Users;AssetsSystem:Assets"/>