2011-08-25 3 views
2

사용자의 유효한 라이선스를 보유하고 있으며 현재 라이선스가 유효하고 변경되었을 수있는 항목을 추가/제거하기 위해 15 분마다 "유효성이 검사됩니다"라는 클래스가 있습니다.MVC2의 캐시 저장소 사용

현재이 컨트롤은 응용 프로그램의 다른 모든 컨트롤러가 상속하는 내 ApplicationController에서 액세스되므로 사용자가 어떤 작업을 수행 할 때마다 유효한 라이센스/권한이 있는지 확인해야합니다.

라이선스 모델 :

public class LicenseModel 
{ 
    public DateTime LastValidated { get; set; } 
    public List<License> ValidLicenses { get; set; } 
    public bool NeedsValidation 
    { 
     get{ return ((DateTime.Now - this.LastValidated).Minutes >= 15);} 
    } 

    //Constructor etc... 
} 

검증 프로세스 :합니다 (와 ApplicationController의 초기화() 메소드 내부에서 발생)

LicenseModel licenseInformation = new LicenseModel();  
if (Session["License"] != null) 
{ 
    licenseInformation = Session["License"] as LicenseModel; 
    if (licenseInformation.NeedsValidation) 
     licenseInformation.ValidLicenses = Service.GetLicenses(); 
     licenseInformation.LastValidated = DateTime.Now; 
     Session["License"] = licenseInformation; 
} 
else 
{ 
    licenseInformation = new LicenseModel(Service.GetLicenses()); 
    Session["License"] = licenseInformation; 
} 

요약 :

보시다시피,이 프로세스는 현재 Session을 사용하여 LicenseModel을 저장하지만 캐시를 사용하여 저장하는 것이 더 쉽고/더 효율적인지 궁금합니다. (또는 아마도 OutputCache?) 그리고 어떻게 구현할 것인가.

답변

1

라이센스가 응용 프로그램 전체에서 사용되고 모든 사용자 세션에 특정한 것이 아닌 경우 캐시가 더 적합 할 것입니다. 캐시는 15 분 만료를 처리 할 수 ​​있으며 더 이상 LicenseModel 클래스의 NeedsValidation 또는 LastValidated 속성이 필요하지 않습니다. 아마도 모델을 함께 사용하지 않고 유효한 라이센스 목록을 다음과 같이 저장할 수 있습니다.

if (HttpContext.Cache["License"] == null) 
{ 
    HttpContext.Cache.Insert("License",Service.GetLicenses(), null, 
    DateTime.Now.AddMinutes(15), Cache.NoSlidingExpiration); 
} 

var licenses = HttpContext.Cache["License"] as List<License>;