ServiceStack ApiKeyAuthProvider在每个请求上触发6个select语句

时间:2017-01-25 14:49:57

标签: c# .net caching servicestack

我有一个配置为使用ApiKeyAuthProvider的网络服务,如下所示:

container.Register<ICacheClient>(new MemoryCacheClient());
container.Register<IAuthRepository>(c => new OrmLiteAuthRepository(c.Resolve<IDbConnectionFactory>(), "UserDb"));
container.Resolve<IAuthRepository>().InitSchema();
Plugins.Add(new AuthFeature(() => new AuthUserSession(),
    new IAuthProvider[]
    {
        new ApiKeyAuthProvider(AppSettings) { ServiceRoutes = new Dictionary<Type, string[]>() }
    }));

我也让OrmLite将SQL转储到日志文件中。

SQL: SELECT "Id", "UserAuthId", "Environment", "KeyType", "CreatedDate", "ExpiryDate", "CancelledDate", "Notes", "RefId", "RefIdStr", "Meta" FROM "ApiKey" WHERE "Id" = @Id
PARAMS: Id=APITOKENHERE 

SQL: SELECT "Id", "UserName", "Email", "PrimaryEmail", "PhoneNumber", "FirstName", "LastName", "DisplayName", "Company", "BirthDate", "BirthDateRaw", "Address", "Address2", "City", "State", "Country", "Culture", "FullName", "Gender", "Language", "MailAddress", "Nickname", "PostalCode", "TimeZone", "Salt", "PasswordHash", "DigestHa1Hash", "Roles", "Permissions", "CreatedDate", "ModifiedDate", "InvalidLoginAttempts", "LastLoginAttempt", "LockedDate", "RecoveryToken", "RefId", "RefIdStr", "Meta" FROM "UserAuth" WHERE "Id" = @Id
PARAMS: Id=1  

SQL: SELECT "Id", "UserAuthId", "Provider", "UserId", "UserName", "FullName", "DisplayName", "FirstName", "LastName", "Company", "Email", "PhoneNumber", "BirthDate", "BirthDateRaw", "Address", "Address2", "City", "State", "Country", "Culture", "Gender", "Language", "MailAddress", "Nickname", "PostalCode", "TimeZone", "RefreshToken", "RefreshTokenExpiry", "RequestToken", "RequestTokenSecret", "Items", "AccessToken", "AccessTokenSecret", "CreatedDate", "ModifiedDate", "RefId", "RefIdStr", "Meta" 
FROM "UserAuthDetails"
WHERE ("UserAuthId" = @0)
PARAMS: @0=1  

SQL: SELECT "Id", "UserName", "Email", "PrimaryEmail", "PhoneNumber", "FirstName", "LastName", "DisplayName", "Company", "BirthDate", "BirthDateRaw", "Address", "Address2", "City", "State", "Country", "Culture", "FullName", "Gender", "Language", "MailAddress", "Nickname", "PostalCode", "TimeZone", "Salt", "PasswordHash", "DigestHa1Hash", "Roles", "Permissions", "CreatedDate", "ModifiedDate", "InvalidLoginAttempts", "LastLoginAttempt", "LockedDate", "RecoveryToken", "RefId", "RefIdStr", "Meta" FROM "UserAuth" WHERE "Id" = @Id
PARAMS: Id=1  

SQL: SELECT "Id", "UserName", "Email", "PrimaryEmail", "PhoneNumber", "FirstName", "LastName", "DisplayName", "Company", "BirthDate", "BirthDateRaw", "Address", "Address2", "City", "State", "Country", "Culture", "FullName", "Gender", "Language", "MailAddress", "Nickname", "PostalCode", "TimeZone", "Salt", "PasswordHash", "DigestHa1Hash", "Roles", "Permissions", "CreatedDate", "ModifiedDate", "InvalidLoginAttempts", "LastLoginAttempt", "LockedDate", "RecoveryToken", "RefId", "RefIdStr", "Meta" FROM "UserAuth" WHERE "Id" = @Id
PARAMS: Id=1  

SQL: SELECT "Id", "UserAuthId", "Provider", "UserId", "UserName", "FullName", "DisplayName", "FirstName", "LastName", "Company", "Email", "PhoneNumber", "BirthDate", "BirthDateRaw", "Address", "Address2", "City", "State", "Country", "Culture", "Gender", "Language", "MailAddress", "Nickname", "PostalCode", "TimeZone", "RefreshToken", "RefreshTokenExpiry", "RequestToken", "RequestTokenSecret", "Items", "AccessToken", "AccessTokenSecret", "CreatedDate", "ModifiedDate", "RefId", "RefIdStr", "Meta" 
FROM "UserAuthDetails"
WHERE ("UserAuthId" = @0)
PARAMS: @0=1  

我期望sessions to be cached并且不会针对每个请求点击数据库。然而,这似乎并不是它正在做的事情。对于我发送给我的API的每个请求,它都会向auth数据库发出相同的6个select语句。我不知道我是不是只是希望它能做一些不打算做的事,或者我在某个地方错过了一件事。

在宏伟的计划中,他们不需要花很长时间才能运行,但无论服务是什么,它总是需要至少200毫秒来运行这些选择。

我有什么遗失的吗?

1 个答案:

答案 0 :(得分:4)

API Key Auth Provider是无状态AuthProvider,默认情况下不会在请求之间缓存任何经过身份验证的用户会话,因此需要在每个请求上重新进行身份验证。

Cached API Key Sessions

但是在ServiceStack最新的v4.5.6版本中,您可以通过指定SessionCacheDuration临时存储Authenticated UserSession来对API Key进行临时存储以减少后续API,从而减少I / O请求的数量并提高API Key Auth Provider Requests的性能密钥请求低至1 DB调用以获取并验证API密钥+ 1高速缓存命中以恢复用户会话,如果您使用默认的内存高速缓存,则意味着它只需要1个I / O调用来进行数据库请求。

可以通过以下方式启用:

Plugins.Add(new AuthFeature(...,
    new IAuthProvider[] {
        new ApiKeyAuthProvider(AppSettings) {
            SessionCacheDuration = TimeSpan.FromMinutes(10),
        }
    }));