如何遍历 ServiceStack 中的所有会话? [英] How do I iterate across all sessions in ServiceStack?

查看:36
本文介绍了如何遍历 ServiceStack 中的所有会话?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们的应用程序在每个公司都有公司和用途.每家公司都有 X 个许可证.我们正在使用类型化会话类,该类包含公司的 id 以及其他用户信息.我需要添加到我们的身份验证过程中,检查当前登录是否会超出公司的许可.我相信这样做的方法是获取所有会话中与当前尝试登录的用户的 companyId 匹配的所有公司 Id 的计数,并将该计数与我们公司表中的可用许可证数量进行比较.这是一种有效的方法吗?如果是这样,请告诉我如何在所有会话中进行查询,因为我还没有弄清楚,而且我对 ServiceStack 还是有点陌生​​.

Our application has companies and uses in each company. Each company has X number of licenses. We are using a typed session class, and that class contains the id of the company along with other user information. I need to add to our authentication process, a check to see if the current login will exceed a company's licenses. I believe that the way to do this is to get a count of all company Ids across all sessions that match the companyId of the user currently trying to log in, and compare that count to the number of available licenses in our company table. Is this a valid approach? If so, please let me know how to query across all sessions, as I haven't figured it out and I'm still somewhat new to ServiceStack.

推荐答案

使用 ICacheClientExtended API

ICacheClientExtended 接口现在支持 GetKeysByPattern API,可让您扫描实现该接口的缓存客户端上的匹配键.目前这是通过以下方式实现的:

Using ICacheClientExtended API's

The ICacheClientExtended interface now supports a GetKeysByPattern API that lets you scan for matching keys on Cache Clients that implement the interface. Currently this is implemented by:

  • MemoryCacheClient
  • OrmLiteCacheClient
  • Redis
  • AWS DynamoDbCacheClient

此 API 现在启用新的 GetAllKeys()GetKeysStartingWith() 扩展方法,现在在 ICacheClient 上可用,以扫描所有缓存键.

This API now enables the new GetAllKeys() and GetKeysStartingWith() extension methods now available on ICacheClient to scan all cache keys.

var prefix = IdUtils.CreateUrn<IAuthSession>(""); //= urn:iauthsession:
var sessionKeys = Cache.GetKeysStartingWith(sessionPattern).ToList();
var userSessions = Cache.GetValues<AuthUserSession>(sessionKeys);
var existingSessions = userSessions.Values.Where(x => x != null).ToList();

其中 existingSessions 将包含活动用户会话的列表.

Where existingSessions will contain list of active User Sessions.

性能将取决于您拥有多少活跃会话,这取决于您网站的受欢迎程度.您可能希望为每个公司保留一组会话 ID,而不是搜索所有键,您可以通过将它们添加到 会话或身份验证事件.

Performance will be determined by how many active sessions you have which could be high depending on popularity of your site. Instead of searching all keys you may want to keep a set of session ids per company that you would look through instead by adding them to a collection in a Session or Auth Event.

此新 API 可从 ServiceStack 的 v4.0.45+ 获得,现在 在 MyGet 上可用.

This new API is available from v4.0.45+ of ServiceStack that's now available on MyGet.

最新的DynamoDbCacheClient 现在位于 MyGet 上的新 ServiceStack.Aws NuGet 包中,该包引用了来自 AWS 开发工具包的最新 v3.* 包.

The latest DynamoDbCacheClient is now in the new ServiceStack.Aws NuGet package on MyGet which references the latest v3.* packages from the AWS SDK.

另一种选择是直接从您的 ICacheClient 访问它们 缓存提供程序 如果它支持全扫描,例如如果您使用 OrmLiteCacheClient 在 RDBMS 中存储会话,您可以通过以下方式访问所有缓存条目:

Another option is access them directly from your ICacheClient Caching provider if it supports full scans, e.g. if you're using OrmLiteCacheClient to store sessions in an RDBMS you can access all Cache Entries with:

var sessionEntries = Db.Select<CacheEntry>(q => 
    q.Where(x => x.Id.StartsWith("urn:iauthsession:")));
var userSessions = sessionEntries.Map(x => 
    Db.Deserialize<AuthUserSession>(x.Data));

如果您使用 Redis 进行缓存,您可以通过以下方式获取:

If you're using Redis for Caching you can get it with:

var sessionKeys = Redis.SearchKeys("urn:iauthsession:*");
var sessions = Redis.GetValues<AuthUserSession>(sessionKeys);

另一种选择是在每个用户登录时通过注册自定义 会话或身份验证事件,您可以在 Redis SET 或 RDBMS 表中维护它们.在同一个注册挂钩中,您可以通过使用 ICacheClient.GetAll API 并传入该公司的会话密钥来验证他们有多少活动会话,例如:

Another option would be to maintain a list of all Session Ids for each Company as each user logs in by registering a custom Session or Auth Event which you could maintain in a Redis SET or RDBMS table. In the same registration hook you can verify how many active Sessions they have by using ICacheClient.GetAll API and passing in the session keys for that company, something like:

var sessions = Cache.GetAll<AuthUserSession>(companySessionKeys);
var activeSessions = sessions.Values.Where(x => x != null).ToList();

这篇关于如何遍历 ServiceStack 中的所有会话?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆