Esempio n. 1
0
        public async Task <UserSnapshot> GetTokenInfoAsync(string token)
        {
            var key = RedisKeyGenerator.Concat(UserMapKey, token);
            var val = await database.StringGetAsync(key);

            return(val.Get <UserSnapshot>());
        }
Esempio n. 2
0
        public async Task <string> LoginAsync(string connectId, string userName, string passwordHash)
        {
            var privateKey = await GetPrivateKeyAsync(connectId);

            if (privateKey is null)
            {
                return(null);
            }
            var val = RSAHelper.RSADecrypt(privateKey, passwordHash);

            if (string.IsNullOrEmpty(val))
            {
                return(null);
            }
            var u = await userManager.FindByNameAsync(userName);

            var ok = await userManager.CheckPasswordAsync(u, val);

            if (ok)
            {
                var key = RedisKeyGenerator.Concat(RSAKey, connectId);
                await database.KeyDeleteAsync(key);

                var identity = new UserSnapshot
                {
                    Email = u.Email,
                    Id    = u.Id,
                    Name  = u.NormalizedUserName
                };
                var tk = await userIdentityService.SetIdentityAsync(identity);

                return(tk);
            }
            return(null);
        }
Esempio n. 3
0
        public async Task <AnfBookshelfItem> GetAsync(long bookshelfId, string address)
        {
            var key      = RedisKeyGenerator.Concat(ReadingKey, bookshelfId, address);
            var entities = await database.HashGetAllAsync(key);

            return(ToBookselfItem(entities));
        }
Esempio n. 4
0
        public async Task <IActionResult> GetEntity([FromQuery] string url)
        {
            if (string.IsNullOrEmpty(url))
            {
                return(BadRequest());
            }
            var key = RedisKeyGenerator.Concat(EntityKey, url);
            var res = memoryCache.Get <AnfComicEntityTruck>(key);

            if (res != null)
            {
                await comicRankService.AddScopeAsync(url);

                return(Ok(res));
            }
            res = await rootFetcher.FetchEntityAsync(url);

            if (res != null)
            {
                await comicRankService.AddScopeAsync(url);

                memoryCache.Set(key, res, new MemoryCacheEntryOptions
                {
                    SlidingExpiration = CacheTime
                });
            }
            return(Ok(res));
        }
Esempio n. 5
0
        public async Task <string> SetIdentityAsync(UserSnapshot snapshot)
        {
            var tk    = Guid.NewGuid().ToString();
            var key   = RedisKeyGenerator.Concat(UserMapKey, tk);
            var bytes = JsonSerializer.SerializeToUtf8Bytes(snapshot);
            await database.StringSetAsync(key, bytes, ExpireTime);

            return(tk);
        }
Esempio n. 6
0
        private Task <IResourceLocker> CreateLockerAsync(string part)
        {
            if (resourceLockerFactory is null)
            {
                throw new NotSupportedException("The IResourceLockerFactory is not provided, the function is not support");
            }
            var key = RedisKeyGenerator.Concat(FetckKey, part, Url);

            return(resourceLockerFactory.CreateLockerAsync(key));
        }
Esempio n. 7
0
        private async Task <string> GetPrivateKeyAsync(string connectId)
        {
            var key             = RedisKeyGenerator.Concat(RSAKey, connectId);
            var privateKeyValue = await database.StringGetAsync(key);

            if (!privateKeyValue.HasValue)
            {
                return(null);
            }
            return(privateKeyValue.ToString());
        }
Esempio n. 8
0
        public async Task <RSAKeyIdentity> FlushRSAKey()
        {
            var rsaKey   = RSAHelper.GenerateRSASecretKey();
            var identity = Guid.NewGuid().ToString();
            var key      = RedisKeyGenerator.Concat(RSAKey, identity);
            await database.StringSetAsync(key, rsaKey.PrivateKey, RSAKeyCacheTime);

            return(new RSAKeyIdentity
            {
                Key = rsaKey.PublicKey,
                Identity = identity
            });
        }
Esempio n. 9
0
        public async Task <AnfBookshelfItem[]> BatchGetAsync(ReadingIdentity[] identities)
        {
            var keys  = identities.Select(x => RedisKeyGenerator.Concat(ReadingKey, x.BookshelfId, x.Address));
            var batch = database.CreateBatch();
            var tasks = keys.Select(x => batch.HashGetAllAsync(x)).ToArray();

            batch.Execute();
            var entities = await Task.WhenAll(tasks);

            return(entities.Where(x => x != null)
                   .Select(x => ToBookselfItem(x))
                   .ToArray());
        }
Esempio n. 10
0
        public async Task <IActionResult> Search([FromQuery] string provider, [FromQuery] string keyword, [FromQuery] int skip = 0, [FromQuery] int take = 20)
        {
            if (searchEngine.Count == 0)
            {
                return(base.Problem("The search engine nothing"));
            }
            var prov = searchEngine[0];

            if (!string.IsNullOrEmpty(provider))
            {
                prov = searchEngine.FirstOrDefault(x => x.Name == provider);
                if (prov is null)
                {
                    prov = searchEngine[0];
                }
            }
            var key = RedisKeyGenerator.Concat(SearchKey, provider, keyword);
            var ds  = memoryCache.Get(key);

            if (ds != null)
            {
                await hotSearchService.AddSearch(keyword);

                return(Ok(ds));
            }
            using var scope = searchEngine.ServiceScopeFactory.CreateScope();
            var eng  = (ISearchProvider)scope.ServiceProvider.GetService(prov);
            var data = await eng.SearchAsync(keyword, skip, take);

            await hotSearchService.AddSearch(keyword);

            var r = new EntityResult <SearchComicResult> {
                Data = data
            };

            memoryCache.Set(key, r, new MemoryCacheEntryOptions
            {
                SlidingExpiration = CacheTime
            });
            return(Ok(r));
        }
Esempio n. 11
0
        public async Task <IActionResult> GetChapter([FromQuery] string url, [FromQuery] string entityUrl)
        {
            if (string.IsNullOrEmpty(url))
            {
                return(BadRequest());
            }
            var key = RedisKeyGenerator.Concat(ChapterKey, url);
            var res = memoryCache.Get <WithPageChapter>(key);

            if (res != null)
            {
                return(Ok(res));
            }
            res = await rootFetcher.FetchChapterAsync(url, entityUrl);

            if (res != null)
            {
                memoryCache.Set(key, res, new MemoryCacheEntryOptions
                {
                    SlidingExpiration = CacheTime
                });
            }
            return(Ok(res));
        }