Beispiel #1
0
            public async Task <string> GetOrCreate(URLShortenDBContext context)
            {
                while (await _cache_req.WaitToReadAsync())
                {
                    while (_cache_req.TryRead(out var key))
                    {
                        if (!_cache.TryGetValue(key, out string cacheEntry))// Look for cache key.
                        {
                            // Key not in cache, so get data.
                            //TODO: Handle null values way more elegantly
                            var urlViewModel = context?.UrlViewModels?
                                               .Where(x => x.ShortAddress == key && x.Active == true)?
                                               .FirstOrDefault();

                            cacheEntry = urlViewModel?.Address ?? "not found";

                            if (cacheEntry != "not found")
                            {
                                _cache.Set(key, cacheEntry);
                            }
                        }

                        return(cacheEntry);
                    }
                }
                //TODO: Think of something better than this
                return("error");
            }
Beispiel #2
0
        public async Task <string> GetOrCreate(string key, URLShortenDBContext context)
        {
            var _channel = Channel.CreateUnbounded <string>(new UnboundedChannelOptions()
            {
                SingleWriter = true,
                SingleReader = false
            });

            var _crequest = new CacheRequest(_channel.Writer, 1);
            var _cset     = new CacheSet(_channel.Reader, 1);
            var _cset2    = new CacheSet(_channel.Reader, 2);
            var _cset3    = new CacheSet(_channel.Reader, 3);

            await _crequest.GetCache(key)
            .ContinueWith(_ => _channel.Writer.Complete());

            var address = await Task.WhenAny(_cset.GetOrCreate(context), _cset2.GetOrCreate(context), _cset3.GetOrCreate(context));

            return(await address);
        }
Beispiel #3
0
        public static void Init(IServiceProvider svc)
        {
            using (var context = new URLShortenDBContext(svc.GetRequiredService <DbContextOptions <URLShortenDBContext> >()))
            {
                //We need to initially set at least one admin here
                if (!context.AdminModel.Any())
                {
                    context.AdminModel.AddRange(

                        new Models.AdminModel
                    {
                        User   = "******",
                        Groups = ""
                    }

                        );
                }

                if (context.UrlViewModels.Any())
                {
                    return;
                }

                context.UrlViewModels.AddRange(

                    new Models.UrlViewModel
                {
                    Active       = true,
                    Address      = "https://mydomain.com",
                    ShortAddress = "QGe8i",
                    UrlHash      = 12345,
                    Created      = DateTime.Now,
                    Title        = "Alachua County",
                    User         = "******"
                }

                    );

                context.SaveChanges();
            }
        }
Beispiel #4
0
        public UrlCache(IServiceProvider svc)
        {
            if (_cache == null)
            {
                _cache = new MemoryCache(new MemoryCacheOptions());

                //constructor should init cache and set it

                using (var context = new URLShortenDBContext(svc.GetRequiredService <DbContextOptions <URLShortenDBContext> >()))
                {
                    var cacheEntries = context.UrlViewModels
                                       .Where(c => c.Active == true)
                                       .Select(c => new { c.ShortAddress, c.Address })
                                       .ToList();

                    foreach (var entry in cacheEntries)
                    {
                        _cache.Set(entry.ShortAddress, entry.Address);
                    }
                }
            }
        }