Beispiel #1
0
        // This method gets called by the runtime. Use this method to add services to the container.
        /// <summary>
        /// Configures the services.
        /// </summary>
        /// <param name="services">The services.</param>
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllersWithViews();
            services.AddMemoryCache();
            //todo check config
            var connection = Configuration["Production:SqliteConnectionString"];


            services.AddDbContext <DatabaseContext>(options =>
                                                    options.UseSqlite(connection)
                                                    );
            var RestoreStoreStyle = Configuration.GetValue <RestoreStoreStyleEnum>("PersistentReStore");

            services.AddScoped <IAllPageURLRepository, AllPageURLRepository>();
            services.AddScoped <IPageURLRepository, PageURLRepository>();



            if (RestoreStoreStyle == RestoreStoreStyleEnum.Db)
            {
                storeStyle = RestoreStoreStyleEnum.Db;
            }

            // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        }
Beispiel #2
0
 //todo context
 /// <summary>
 /// Initializes a new instance of the <see cref="HomeController"/> class.
 /// </summary>
 /// <param name="logger">The logger.</param>
 /// <param name="urlcache">The urlcache.</param>
 /// <param name="repository">The repository.</param>
 /// <param name="allrepository">The allrepository.</param>
 /// <param name="configuration">The configuration.</param>
 public HomeController(ILogger <HomeController> logger, IMemoryCache urlcache, IPageURLRepository repository, IAllPageURLRepository allrepository, IConfiguration configuration)
 {
     _logger          = logger;
     _localCache      = urlcache;
     _repository      = repository;
     _allrepository   = allrepository;
     _configuration   = configuration;
     _persistentStore = Enum.Parse <RestoreStoreStyleEnum>
                            (_configuration.GetValue <string>("PersistentReStore"));
 }
Beispiel #3
0
        /// <summary>
        /// Stores the specified local cache.
        /// </summary>
        /// <param name="_localCache">The local cache.</param>
        /// <param name="storestyle">The storestyle.</param>
        /// <param name="alldatabase">The alldatabase.</param>
        /// <param name="database">The database.</param>
        public static void Store(IMemoryCache _localCache, RestoreStoreStyleEnum storestyle, IAllPageURLRepository alldatabase, IPageURLRepository database)
        {
            bool blanklist = true;

            var _allKeys = new AllPageUrls();

            _localCache.TryGetValue("AllKeys", out _allKeys);



            if (storestyle == RestoreStoreStyleEnum.File)
            {
                using FileStream fs = File.Create("storevalues.json");

                var    _output = JsonConvert.SerializeObject(_allKeys.AllUrls);
                byte[] bytes   = Encoding.UTF8.GetBytes(_output);
                fs.Write(bytes, 0, bytes.Length);
            }
            if (storestyle == RestoreStoreStyleEnum.Db)
            {
                var allDbkey  = alldatabase.GetAllPageURL();
                var allDbkeys = database.GetAllPageURL();

                Guid originalId = Guid.NewGuid();

                if (allDbkey.Result != null)
                {
                    originalId = allDbkey.Result.Id;
                    blanklist  = false;
                }


                var Allkeys = new AllPageUrls()
                {
                    Id = originalId, AllUrls = new List <PageURL>()
                };

                Allkeys.AllUrls.AddRange(_allKeys.AllUrls);
                if (blanklist)
                {
                    alldatabase.SetAllUrls(Allkeys);
                }
                else
                {
                    alldatabase.Update(Allkeys);
                }
            }
        }
Beispiel #4
0
        /// <summary>
        /// Loads the specified local cache.
        /// </summary>
        /// <param name="_localCache">The local cache.</param>
        /// <param name="dbAllPageUrlRepository">The database all page URL repository.</param>
        /// <param name="dbPageUrlRepository">The database page URL repository.</param>
        /// <param name="restoresstyle">The restoresstyle.</param>
        public static void Load(IMemoryCache _localCache, IAllPageURLRepository dbAllPageUrlRepository, IPageURLRepository dbPageUrlRepository, RestoreStoreStyleEnum restoresstyle)
        {
            if (restoresstyle == RestoreStoreStyleEnum.File)
            {
                if (File.Exists(@"storevalues.json"))
                {
                    List <PageURL> _allKeys = JsonConvert.DeserializeObject <List <PageURL> >(File.ReadAllText(@"storevalues.json"));
                    _localCache.Set("AllKeys", _allKeys);
                }
            }

            if (restoresstyle == RestoreStoreStyleEnum.Db)
            {
                var _allKeys = dbAllPageUrlRepository.GetAllPageURL().Result;
                _localCache.Set("AllKeys", _allKeys);
                if (_allKeys != null)
                {
                    if (_allKeys.AllUrls != null)
                    {
                        _allKeys.AllUrls.ForEach(item =>
                        {
                            _localCache.Set(item.ShortURL, item);
                        });
                    }
                }
            }
        }