Exemple #1
0
        public async Task <ISet <string> > GetRolesAsync(string userId)
        {
            if (userId == null)
            {
                return(new HashSet <string>());
            }

            var user = await this._userManager.FindByIdAsync(userId);

            if (user == null)
            {
                return(new HashSet <string>());
            }

            if (this._roles != null)
            {
                return(_roles);
            }

            var cacheKey = UserRolesFetcher.CacheKey(userId);

            if (!this._cache.TryGetValue(cacheKey, out this._roles))
            {
                this._roles = new HashSet <string>(await this._userManager.GetRolesAsync(user));

                var cacheEntryOptions = new MemoryCacheEntryOptions();
                cacheEntryOptions.SetSlidingExpiration(TimeSpan.FromMinutes(5));

                this._cache.Set(cacheKey, this._roles, cacheEntryOptions);
            }

            return(_roles);
        }
Exemple #2
0
 public void ClearCache(string userId)
 {
     this._cache.Remove(UserRolesFetcher.CacheKey(userId));
 }