public void MemoryCacheProviderGetOrCreateCacheUsageTest()
        {
            EFCacheExtensions.SetCacheProvider(MemoryCacheProvider.GetInstance());
            using (ProductContext context = new ProductContext())
            {
                var query = context.Products
                            .OrderBy(one => one.ProductNumber)
                            .Where(one => one.IsActive).AsCacheable();

                Assert.AreEqual(2, query.Count(), "Should have 2 rows");

                SQLCommandHelper.ExecuteNonQuery("Update Products Set IsActive = 0");

                query = context.Products
                        .OrderBy(one => one.ProductNumber)
                        .Where(one => one.IsActive).AsCacheable();
                Assert.AreEqual(2, query.Count(), "Should have 2 rows");


                IQueryable <Product> cleanupQuery = context.Products
                                                    .OrderBy(one => one.ProductNumber)
                                                    .Where(one => one.IsActive);

                EFCacheExtensions.RemoveFromCache <Product>(cleanupQuery);

                query = context.Products
                        .OrderBy(one => one.ProductNumber)
                        .Where(one => one.IsActive).AsCacheable();
                Assert.AreEqual(0, query.Count(), "Should have 0 rows");

                EFCacheExtensions.RemoveFromCache <Product>(cleanupQuery);
            }
        }
        public void MemoryCacheProviderGetOrCreateCacheTest()
        {
            MemoryCacheProvider  target = MemoryCacheProvider.GetInstance();
            IQueryable <Product> query  = null;

            using (ProductContext context = new ProductContext())
            {
                query = context.Products.OrderBy(one => one.ProductNumber).Where(one => one.IsActive);

                target.RemoveFromCache <Product>(query);

                var actual = target.GetOrCreateCache <Product>(query);
                Assert.AreEqual(2, actual.Count(), "Should have 2 rows");

                SQLCommandHelper.ExecuteNonQuery("Update Products Set IsActive = 0");

                actual = target.GetOrCreateCache <Product>(query);
                Assert.AreEqual(2, actual.Count(), "Should have 2 rows");

                target.RemoveFromCache <Product>(query);

                actual = target.GetOrCreateCache <Product>(query);
                Assert.AreEqual(0, actual.Count(), "Should have 0 rows");

                target.RemoveFromCache <Product>(query);
            }
        }
        public void MemoryCacheProviderGetOrCreateCacheWithExpirationTest()
        {
            EFCacheExtensions.SetCacheProvider(MemoryCacheProvider.GetInstance());
            MemoryCacheProvider  target = MemoryCacheProvider.GetInstance();
            IQueryable <Product> query  = null;

            using (ProductContext context = new ProductContext())
            {
                query = context.Products.OrderBy(one => one.ProductNumber).Where(one => one.IsActive);
                target.RemoveFromCache <Product>(query);

                TimeSpan cacheDuration = TimeSpan.FromSeconds(3);

                var actual = target.GetOrCreateCache <Product>(query, cacheDuration);
                Assert.AreEqual(2, actual.Count(), "Should have 2 rows");
                SQLCommandHelper.ExecuteNonQuery("Update Products Set IsActive = 0");
                actual = target.GetOrCreateCache <Product>(query, cacheDuration);
                Assert.AreEqual(2, actual.Count(), "Should have 2 rows");
                Thread.Sleep(4000);

                actual = target.GetOrCreateCache <Product>(query, cacheDuration);
                Assert.AreEqual(0, actual.Count(), "Should have 0 rows");

                target.RemoveFromCache <Product>(query);
            }
        }
Exemplo n.º 4
0
        public void TestMemoryCacheProvider()
        {
            CacheProviderManager.CacheProvider = MemoryCacheProvider.GetInstance();
            var cacheProvider = CacheProviderManager.CacheProvider;
            var cacheSettings = new CacheSettings(new TimeSpan(0, 0, 0, 0, 20), true);

            cacheProvider.Set("Test", "Hello", cacheSettings);

            var data = cacheProvider.Get <string>("Test");

            Assert.Equal("Hello", data.Item);

            Thread.Sleep(20);

            data = cacheProvider.Get <string>("Test");
            Assert.Null(data);
        }
Exemplo n.º 5
0
        public AppSettings()
        {
            try
            {
                var memoryCacheProvider   = MemoryCacheProvider.GetInstance();
                var declaringTypeFullName = MethodBase.GetCurrentMethod()?.DeclaringType?.FullName;
                var filePathKey           = $"{declaringTypeFullName}{@".FilePath"}";
                var filePath = (object)memoryCacheProvider.Get(filePathKey);
                if (null == filePath)
                {
                    AppSettingsRepository?.MergeAndCopyToUserDirectory(this);
                    memoryCacheProvider.Put(filePathKey, FilePath, TimeSpan.FromDays(1));
                }

                if (null != FileName && null != UserProfileDirectory)
                {
                    FilePath = (string)(filePath ?? Path.Combine(UserProfileDirectory, FileName));
                }

                var useGlobalDatabaseConnectionSettingsKey =
                    $"{declaringTypeFullName}{@".UseGlobalDatabaseConnectionSettings"}";
                var useGlobalDatabaseConnectionSettings =
                    (object)memoryCacheProvider.Get(useGlobalDatabaseConnectionSettingsKey);
                if (null == useGlobalDatabaseConnectionSettings)
                {
                    memoryCacheProvider.Put(useGlobalDatabaseConnectionSettingsKey, UseGlobalDatabaseConnectionSettings,
                                            TimeSpan.FromDays(1));
                    if (UseGlobalDatabaseConnectionSettings)
                    {
                        var appSettingsModel = new NetAppCommon.AppSettings.Models.AppSettings();
                        ConnectionString = appSettingsModel.ConnectionString;
                        AppSettingsRepository?.MergeAndSave(this);
                    }
                }
            }
            catch (Exception e)
            {
                _log4Net.Error($"\n{e.GetType()}\n{e.InnerException?.GetType()}\n{e.Message}\n{e.StackTrace}\n", e);
            }
        }
Exemplo n.º 6
0
        public AppSettings()
        {
            try
            {
                var memoryCacheProvider = MemoryCacheProvider.GetInstance();
                var filePathKey         = $"{MethodBase.GetCurrentMethod()?.DeclaringType?.FullName}.FilePath";
                var filePath            = (object)memoryCacheProvider.Get(filePathKey);
                if (null == filePath)
                {
                    var appSettingsSetupFilePath         = Path.Combine(BaseDirectory !, SetupFileName !);
                    var appSettingsUserFilePath          = Path.Combine(UserProfileDirectory !, FileName !);
                    var appSettingsFilePath              = FilePath;
                    var appSettingsSetupConnectionString =
                        new AppSettings(appSettingsSetupFilePath !).GetConnectionString();
                    var appSettingsUserConnectionString =
                        new AppSettings(appSettingsUserFilePath !).GetConnectionString();
                    var appSettingsConnectionString =
                        new AppSettings(appSettingsFilePath !).GetConnectionString();

                    if (!string.IsNullOrWhiteSpace(appSettingsUserFilePath) && !File.Exists(appSettingsUserFilePath))
                    {
                        AppSettingsRepository?.CopyToUserDirectory(this);
                    }

                    try
                    {
                        AppSettingsRepository?.MergeAndSave(appSettingsSetupFilePath, appSettingsUserFilePath);
                    }
                    catch (Exception e)
                    {
                        _log4Net.Error($"\n{e.GetType()}\n{e.InnerException?.GetType()}\n{e.Message}\n{e.StackTrace}\n",
                                       e);
                    }

                    try
                    {
                        if (File.Exists(appSettingsSetupFilePath))
                        {
                            File.Delete(appSettingsSetupFilePath);
                        }
                    }
                    catch (Exception e)
                    {
                        _log4Net.Error($"\n{e.GetType()}\n{e.InnerException?.GetType()}\n{e.Message}\n{e.StackTrace}\n",
                                       e);
                    }

                    try
                    {
                        AppSettingsRepository?.MergeAndSave(appSettingsUserFilePath, appSettingsFilePath);
                    }
                    catch (Exception e)
                    {
                        _log4Net.Error($"\n{e.GetType()}\n{e.InnerException?.GetType()}\n{e.Message}\n{e.StackTrace}\n",
                                       e);
                    }

                    try
                    {
                        AppSettingsRepository?.MergeAndSave(appSettingsFilePath, appSettingsUserFilePath);
                    }
                    catch (Exception e)
                    {
                        _log4Net.Error($"\n{e.GetType()}\n{e.InnerException?.GetType()}\n{e.Message}\n{e.StackTrace}\n",
                                       e);
                    }

                    FilePath = File.Exists(appSettingsUserFilePath) ? appSettingsUserFilePath : FilePath;

                    if (null != AppSettingsRepository)
                    {
                        if (!string.IsNullOrWhiteSpace(appSettingsSetupConnectionString) &&
                            AppSettingsRepository.MssqlCheckConnectionString(appSettingsSetupConnectionString))
                        {
                            ConnectionString = appSettingsSetupConnectionString;
                            AppSettingsRepository?.SaveAsync(this);
                        }
                        else if (!string.IsNullOrWhiteSpace(appSettingsUserConnectionString) &&
                                 AppSettingsRepository.MssqlCheckConnectionString(appSettingsUserConnectionString))
                        {
                            ConnectionString = appSettingsUserConnectionString;
                            AppSettingsRepository?.SaveAsync(this);
                        }
                        else if (!string.IsNullOrWhiteSpace(appSettingsConnectionString) &&
                                 AppSettingsRepository.MssqlCheckConnectionString(appSettingsConnectionString))
                        {
                            ConnectionString = appSettingsConnectionString;
                            AppSettingsRepository?.SaveAsync(this);
                        }
                    }

                    memoryCacheProvider.Put(filePathKey, FilePath, TimeSpan.FromDays(1));
                }

                if (null != UserProfileDirectory && null != FileName)
                {
                    FilePath = (string)(filePath ?? Path.Combine(UserProfileDirectory !, FileName !));
                }
            }
            catch (Exception e)
            {
                _log4Net.Error($"\n{e.GetType()}\n{e.InnerException?.GetType()}\n{e.Message}\n{e.StackTrace}\n", e);
            }
        }