我们的项目中有一个包含缓存信息的类:
public class SOLECache
{
public DateTime CreatedDTM { get; set; }
public int UserID { get; set; }
public int MaxAgeSeconds { get; set; }
public object Data { get; set; }
}
其中属性 Data 保存我们要缓存的对象,可以是任何类型。因为我们有一个包含多个 Web 服务器的生产环境,所以我们正在尝试使用 Redis 和 ServiceStack 来处理缓存。现在缓存已正确存储在 Redis 服务器上,并且该部分工作正常。问题是当我们尝试获取缓存时:
_cacheClient.Get<AppUser>("someCacheKey");
我们的 Data 属性总是得到一个 NULL 值。所以基本上很明显,数据以正确的键存储在 Redis 服务器上,但是当我们检索它时,我们得到了除了始终为 NULL 的 Data 属性之外的所有内容。
谁能告诉我为什么会这样?可能是因为我们存储在 data 属性中的对象有一些只读属性和一些方法——它不是一个干净的 POCO?这是正在存储的对象的示例结构:
public class AppUser
{
public int UserId {get; set;}
public string FirstName {get; set;}
public string LastName {get; set;}
public string FullName
{
get {
return FirstName + " " + LastName;
}
}
public void DoSomething()
{
// some method logic here...
}
}
此类是否会导致 ServiceStack.RedisClient 无法正确序列化 AppUser 实例?
我们在 Redis 中设置缓存的方式是:
_cacheClient.Set("someCacheKey", soleCache, DateTime.UtcNow.AddSeconds(soleCache.MaxAgeSeconds))
其中,soleCache 是 SOLECache 的一个实例,具有填充的 Data 属性。Redis 上存储的数据如下:
{"CreatedDTM":"2013-06-28T13:45:56.5091664-04:00","UserID":123,"MaxAgeSeconds":60,"Data":{"FullName":"John M Doe","FirstName":"John","MiddleName":"M","LastName":"Doe","Email":"me@here.com","BackupEmail":"me@there.com","GradYear":null,"PostalCode":"12345 ","Location":"12345 ","City":null,"IsValid":true,"Active":true,"AccessLevel":10,"CurrentApp":0,"CurrentURL":"http://localhost:4387/","RequestMethod":"GET","IsMobile":false,"IsInApp":false,"ServerName":"ITSWEB-JMDOE","ID":40117,"IsAdmin":true,"Theme":"basic","ExpirationDT":null,"WebFolderName":"jmdoe","ProgramCD":null,"BranchCD":"MBIM","RoleOverride":0,>"CurrentAppRoleID":0,"EnableNativeMailFLG":false,"PasswordStatus":"GOOD","CampusCD":"M","Campus":"Smallville","DisclaimerDate":"2012-07-02T10:55:54.297","SecurityQuestion":null,"ClassificationMSK":4,"IsLinkedToCurrentApp":false,"IsBlockedFromCurrentApp":false,"IsBlockedMessageText":null,"CurrentAppRoleName":null,"AssignedAppRoleID":0,"AppSmallGroup":0,"PermissionsArray":[],"AdminPermissionsArray":[],"ViewLevel":[],"LastLoginDTM":"2013-06-27T16:28:23.21","Session":{"SessionID":"1923dac2-f3af-4dc5-ad51-7202fc077ba3","SessionCreatedDTM":"2013-06-28T09:03:13.12","SessionTimeout":60,"SessionUserID":40117,"SessionCurrentApp":0,"SessionLastViewDTM":"2013-06-28T13:24:37.69","SessionUserAgent":"Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.52 Safari/537.36","SessionIP":"127.0.0.1","SessionLastURL":"http://localhost:4387/Home/CacheInfo","SessionServer":"ITSWEB-JMDOE","LastLoginDTM":null,"SessionLoginMethod":null,"ADUserName":null,"ADPassword":null,"Errors":{},"HasErrors":false,"ModelState":{}},"CurrentInstanceID":0,"CurrentPageHasNotes":false,"CurrentPageIsFavorited":false,"BrowserBrand":"Chrome","BrowserVersion":"28.0","BrowserPlatform":"Windows 8","BrowserValid":true,"Favorites":[],"FullNameWithEmail":"John M Doe (jmdoe@here.com)","FullNameWithoutMiddleName":"John Doe","IsStudent":true,"Errors":{},"HasErrors":false,"ModelState":{}}}
我们已将问题追溯到 ServiceStack.Text.JsonSerializer 方法,该方法由于某种原因无法正确序列化此对象。当我们用 NewtonSoft 的 Json Serializer 替换此方法时,它工作正常......似乎是 ServiceStack 的问题,但我可能错了......