2017-10-16 3 views
0

私はこのMicrosoftドキュメントCaching Support for WCF Web HTTP Servicesを見てきましたが、ユーザーリストのキャッシュを作成できました。
これは、それがどのように見えるかです:IDに応じたWCF RESTキャッシュリスト

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を使用しないようにします。以下のための最初の呼び出しは、次の5分間、次いでユーザーIDである場合には、このユーザーの資産を返す(かかわらず、ユーザのヌルである、または他の任意の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

soultionはweb.config

<add name="CacheFor5MinutesByUser" duration="300" varyByParam="userId" sqlDependency="AssetsSystem:Users;AssetsSystem:Assets"/> 
veryByParamよう userIdを追加しました