当前位置: 首页>>代码示例>>C#>>正文


C# Cache.Set方法代码示例

本文整理汇总了C#中Cache.Set方法的典型用法代码示例。如果您正苦于以下问题:C# Cache.Set方法的具体用法?C# Cache.Set怎么用?C# Cache.Set使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Cache的用法示例。


在下文中一共展示了Cache.Set方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Initialize

        public async void Initialize()
        {
            var cacheContainer = new CacheContainer();
            cacheContainer.Register<ILogger, TestLogger>();
            cacheContainer.Register<IVersionProvider, TestVersionProvider>().WithValue("version", new Version("1.0"));
            cacheContainer.Register<IStorage, TestStorage>();
            cacheContainer.Register<ISerializer, ProtoBufSerializer>().WithDependency("storage", typeof(IStorage).FullName).WithValue("userTypes", null);

            var cacheConfiguration = new CacheConfiguration(900, 6, 800, 5);

            _cache = new Cache(cacheContainer, cacheConfiguration);
            await _cache.Initialize();
            await _cache.Set("stringKey1", "stringValue1");
            Thread.Sleep(20);
            await _cache.Set("stringKey2", "stringValue2");
            Thread.Sleep(20);
            await _cache.Set("byteKey3", new byte[]{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24});
            Thread.Sleep(20);
            await _cache.Set("stringKey4", "stringValue4");
            Thread.Sleep(20);
            await _cache.Set("Int32Key", 42);
            Thread.Sleep(20);
            await _cache.Set("DateTimeKey", _dateTime);
            Thread.Sleep(20);
            await _cache.Set("floatKey", 13.37f);
            Thread.Sleep(20);
            await _cache.Set("decimalKey", 13.37m);

            await _cache.SaveMappingsAndCheckLimits(null);
        }
开发者ID:IvanLeonenko,项目名称:windows-cache,代码行数:30,代码来源:When_overall_and_in_memory_size_and_quantity_limits_exceeded.cs

示例2: Initialize

        public async void Initialize()
        {
            _cacheContainer = new CacheContainer();
            _cacheContainer.Register<ILogger, TestLogger>();
            _cacheContainer.Register<IVersionProvider, TestVersionProvider>().WithValue("version", new Version("1.0"));
            _cacheContainer.Register<IStorage, TestStorage>().AsSingleton();
            _cacheContainer.Register<ISerializer, ProtoBufSerializer>().WithDependency("storage", typeof(IStorage).FullName).WithValue("userTypes", null);

            var cacheConfiguration = new CacheConfiguration(2048, 6, 2048, 5);
            _cache = new Cache(_cacheContainer, cacheConfiguration);
            await _cache.Initialize();
            await _cache.Set("key1", "stringValue");
            await _cache.Set("key2", 42);
            await _cache.Set("key3", new byte[] { 12, 23, 34 });
        }
开发者ID:IvanLeonenko,项目名称:windows-cache,代码行数:15,代码来源:When_cache_entry_added.cs

示例3: should_keep_cache_if_versions_same

        public async Task should_keep_cache_if_versions_same()
        {
            var cacheConfiguration = new CacheConfiguration(1024, 5, 1024, 5);

            var cacheContainer = InitializeCacheContainer();
            var storage = (TestStorage)cacheContainer.Resolve<IStorage>();

            cacheContainer.Register<IVersionProvider, TestVersionProvider>().WithValue("version", new Version(1, 1));

            using (var cache = new Cache(cacheContainer, cacheConfiguration))
            {
                await cache.Initialize();
                //when at least one value set cache is written
                await cache.Set("some_entry", 42);
            }

            //cache should not be cleanued up if versions in storage and executing assembly differ
            cacheContainer.Register<IVersionProvider, TestVersionProvider>().WithValue("version", new Version(1, 1));
            using (var cache = new Cache(cacheContainer, cacheConfiguration))
            {
                await cache.Initialize();
                storage.KeyToStreams.Should().NotBeEmpty();
                cache.Get<Int32>("some_entry").Result.Value.Should().Be(42);
            }
        }
开发者ID:IvanLeonenko,项目名称:windows-cache,代码行数:25,代码来源:When_version_is_stored_in_cache.cs

示例4: Initialize

        public async void Initialize()
        {
            var cacheContainer = new CacheContainer();
            cacheContainer.Register<ILogger, TestLogger>();
            cacheContainer.Register<IVersionProvider, TestVersionProvider>().WithValue("version", new Version("1.0"));
            cacheContainer.Register<IStorage, TestStorage>();
            cacheContainer.Register<ISerializer, ProtoBufSerializer>().WithDependency("storage", typeof(IStorage).FullName).WithValue("userTypes", null);

            var cacheConfiguration = new CacheConfiguration(2048, 6, 1024, 6);

            _cache = new Cache(cacheContainer, cacheConfiguration);
            await _cache.Initialize();
            await _cache.Set("strinKey1", "stringValue1");
            await _cache.Set("strinKey2", "stringValue2");
            await _cache.Set("someBytes", new byte[] { 12, 32, 43 });
            await _cache.Set("Int32Key1", 1);
            await _cache.Set("dateTimeKey1", _dateTime);

        }
开发者ID:IvanLeonenko,项目名称:windows-cache,代码行数:19,代码来源:When_limits_not_exceeded.cs

示例5: m_InterProcess_RegisterRequestedEvent

        bool m_InterProcess_RegisterRequestedEvent(object sender, ApplicationInstanceEventArgs e)
        {
            Cache c = new Cache(false);
            if (c.Exists("locked") && c.Get<bool>("locked"))
                return false;

            if (!c.Exists("instances"))
                c.Set<ApplicationInstanceList>("instances", new ApplicationInstanceList());
            ApplicationInstanceList list = c.Get<ApplicationInstanceList>("instances");
            if (list.Count(v => v.FilesystemPath == e.LocalPath) > 0)
                return false;
            list.Add(new ApplicationInstance
            {
                LastCheckTime = new DateTime(1970, 1, 1),
                FilesystemPath = e.LocalPath,
                UpdateUrl = e.UpdateURI.ToString()
            });
            c.Set<ApplicationInstanceList>("instances", list);
            return true;
        }
开发者ID:hach-que,项目名称:Pivot.Update,代码行数:20,代码来源:Manager.cs

示例6: CheckAddWithSize

        public void CheckAddWithSize()
        {
            var cache = new Cache<ByteArray>(500 * 1024, ba => ba.Length);

            int size = 0;
            for (int i = 0; i < 100; i++) {
                var key = ByteArray.Random(40).ToString();
                var val = ByteArray.Random(256);
                cache.Set(key, val);
                size += val.Length;
                Assert.AreEqual(size, cache.CurrentSize);
            }
        }
开发者ID:gnoso,项目名称:razordb,代码行数:13,代码来源:CacheTests.cs

示例7: Initialize

        public async void Initialize()
        {
            var cacheContainer = new CacheContainer();
            cacheContainer.Register<ILogger, TestLogger>();
            cacheContainer.Register<IVersionProvider, TestVersionProvider>().WithValue("version", new Version("1.0"));
            cacheContainer.Register<IStorage, TestStorage>();
            cacheContainer.Register<ISerializer, ProtoBufSerializer>().WithDependency("storage", typeof(IStorage).FullName).WithValue("userTypes", null);

            var cacheConfiguration = new CacheConfiguration(500, 10, 500, 5);

            _cache = new Cache(cacheContainer, cacheConfiguration);
            await _cache.Initialize();
            await _cache.Set("stringKey1", "stringValue1");
            Thread.Sleep(20);
            await _cache.Set("stringKey2", "stringValue2");
            Thread.Sleep(20);
            await _cache.Set("stringKey3", "stringValue3");
            Thread.Sleep(20);
            await _cache.Set("stringKey4", "stringValue4");
            Thread.Sleep(20);
            await _cache.Set("Int32Key", 42);
            await _cache.SaveMappingsAndCheckLimits(null);
        }
开发者ID:IvanLeonenko,项目名称:windows-cache,代码行数:23,代码来源:When_overall_and_in_memory_size_limits_exceeded.cs

示例8: Initialize

        public async void Initialize()
        {
            var cacheContainer = new CacheContainer();
            cacheContainer.Register<ILogger, TestLogger>();
            cacheContainer.Register<IVersionProvider, TestVersionProvider>().WithValue("version", new Version("1.0"));
            cacheContainer.Register<IStorage, TestStorage>();
            cacheContainer.Register<ISerializer, ProtoBufSerializer>().WithDependency("storage", typeof(IStorage).FullName).WithValue("userTypes", null);

            var cacheConfiguration = new CacheConfiguration(2048, 6, 2048, 5);

            _cache = new Cache(cacheContainer, cacheConfiguration);
            await _cache.Initialize();
            await _cache.Set("key1", "string1", TimeSpan.FromMilliseconds(1));
            
            Thread.Sleep(10);
        }
开发者ID:IvanLeonenko,项目名称:windows-cache,代码行数:16,代码来源:When_cache_entry_expired.cs

示例9: CheckLRU

        public void CheckLRU()
        {
            int limit = 100 * 256;
            var cache = new Cache<ByteArray>(limit, ba => ba.Length);
            var items = new List<string>();

            for (int i = 0; i < 100; i++) {
                var key = ByteArray.Random(40).ToString();
                var val = ByteArray.Random(256);
                cache.Set(key, val);

                items.Add(key);
            }

            ByteArray output;
            // Touch the first 50 items again
            for (int i = 0; i < 50; i++) {
                Assert.IsTrue(cache.TryGetValue(items[i], out output));
            }

            // Add 10 more items
            for (int i = 0; i < 10; i++) {
                var key = ByteArray.Random(40).ToString();
                var val = ByteArray.Random(256);
                cache.Set(key, val);

                items.Add(key);
            }

            // First 50 items should still be there
            for (int i = 0; i < 50; i++) {
                Assert.IsTrue(cache.TryGetValue(items[i], out output));
            }

            // Next 10 items should be evicted
            for (int i = 50; i < 60; i++) {
                Assert.IsFalse(cache.TryGetValue(items[i], out output));
            }

            // Next 50 items should still be there
            for (int i = 60; i < 110; i++) {
                Assert.IsTrue(cache.TryGetValue(items[i], out output));
            }

            Assert.GreaterOrEqual(limit, cache.CurrentSize);
        }
开发者ID:gnoso,项目名称:razordb,代码行数:46,代码来源:CacheTests.cs

示例10: BasicAdd

        public void BasicAdd()
        {
            var cache = new Cache<ByteArray>(500 * 1024, ba => ba.Length );
            var items = new List<KeyValuePair<string, ByteArray>>();

            for (int i = 0; i < 100; i++) {
                var key = ByteArray.Random(40).ToString();
                var val = ByteArray.Random(256);
                cache.Set(key, val);
                items.Add(new KeyValuePair<string, ByteArray>(key, val));
            }

            ByteArray oval;
            for (int i = 0; i < 100; i++) {
                var data = items[i];
                Assert.IsTrue(cache.TryGetValue(data.Key, out oval));
                Assert.AreEqual(data.Value, oval);
            }
            Assert.IsFalse(cache.TryGetValue(ByteArray.Random(40).ToString(), out oval));
        }
开发者ID:gnoso,项目名称:razordb,代码行数:20,代码来源:CacheTests.cs

示例11: CheckOverSizeLimit

        public void CheckOverSizeLimit()
        {
            int limit = 100 * 256;
            var cache = new Cache<ByteArray>(limit, ba => ba.Length);
            var items = new List<string>();

            int size = 0;
            for (int i = 0; i < 200; i++) {
                var key = ByteArray.Random(40).ToString();
                var val = ByteArray.Random(256);
                cache.Set(key, val);
                size += val.Length;

                items.Add(key);
            }

            Assert.GreaterOrEqual(limit, cache.CurrentSize);
            ByteArray output;
            // First 100 items should have been evicted
            for (int i = 0; i < 100; i++) {
                Assert.IsFalse(cache.TryGetValue(items[i], out output));
            }

            // Next 100 items should still be there
            for (int i = 100; i < 200; i++) {
                Assert.IsTrue(cache.TryGetValue(items[i], out output));
            }
        }
开发者ID:gnoso,项目名称:razordb,代码行数:28,代码来源:CacheTests.cs

示例12: Main

        static void Main(string[] args)
        {
            if (args.Length < 1)
            {
                Console.WriteLine("must have at least 1 argument.");
                return;
            }

            switch (args[0])
            {
                case "create":
                    {
                        if (args.Length < 2)
                        {
                            Console.WriteLine("must have at least 2 arguments (create <appname>).");
                            return;
                        }

                        string appname = args[1];
                        Cache c = new Cache(false);

                        if (c.Exists("server/" + appname))
                        {
                            Console.WriteLine("this app is already created.");
                            return;
                        }

                        if (!File.Exists(".pvdeploy"))
                        {
                            Console.WriteLine("no .pvdeploy file found; please create one.");
                            return;
                        }

                        FileFilter filter = FileFilterParser.Parse(".pvdeploy", GetRecursiveFilesInCwd());
                        foreach (KeyValuePair<string, string> kv in filter)
                        {
                            // Key is original filename.
                            // Value is filename to store as.
                            Hash hash = Hash.FromFile(Path.Combine(Environment.CurrentDirectory, kv.Key));
                            Console.WriteLine(Hash.Empty.ToString() + " => " + hash.ToString() + " " + kv.Value);
                            c.Set<Hash>("server/" + appname + "/hashes/" + kv.Value, hash);
                            if (c.Exists("server/" + appname + "/store/" + kv.Value))
                                c.Delete("server/" + appname + "/store/" + kv.Value);
                            File.Copy(Path.Combine(Environment.CurrentDirectory, kv.Key), c.GetFilePath("server/" + appname + "/store/" + kv.Value));
                        }

                        return;
                    }
                case "test-pvdeploy":
                    {
                        if (args.Length < 1)
                        {
                            Console.WriteLine("must have at least 1 argument (test-pvdeploy).");
                            return;
                        }

                        if (!File.Exists(".pvdeploy"))
                        {
                            Console.WriteLine("no .pvdeploy file found; please create one.");
                            return;
                        }

                        FileFilter filter = FileFilterParser.Parse(".pvdeploy", GetRecursiveFilesInCwd());
                        foreach (KeyValuePair<string, string> kv in filter)
                        {
                            // Key is original filename.
                            // Value is filename to store as.
                            Console.WriteLine(kv.Key + " => " + kv.Value);
                        }

                        return;
                    }
                case "flash":
                    {
                        if (args.Length < 2)
                        {
                            Console.WriteLine("must have at least 2 arguments (flash <appname>).");
                            return;
                        }

                        string appname = args[1];
                        Cache c = new Cache(false);

                        if (!c.Exists("server/" + appname))
                        {
                            Console.WriteLine("use create to create this app first.");
                            return;
                        }

                        if (!File.Exists(".pvdeploy"))
                        {
                            Console.WriteLine("no .pvdeploy file found; please create one.");
                            return;
                        }

                        // Find all files in the current working directory.
                        diff_match_patch dmf = new diff_match_patch();
                        FileFilter filter = FileFilterParser.Parse(".pvdeploy", GetRecursiveFilesInCwd());
                        foreach (KeyValuePair<string, string> kv in filter)
                        {
//.........这里部分代码省略.........
开发者ID:hach-que,项目名称:Pivot.Update,代码行数:101,代码来源:Program.cs

示例13: PerformUpdate

        private void PerformUpdate(bool initial)
        {
            Cache c = new Cache(false);
            this.m_DualLog.WriteEntry("Periodic update check has started.");

            // Find all application instances.
            try
            {
                if (!c.Exists("instances"))
                    c.Set<ApplicationInstanceList>("instances", new ApplicationInstanceList());
                ApplicationInstanceList list = c.Get<ApplicationInstanceList>("instances");
                this.m_DualLog.WriteEntry("Application instance list retrieved with " + list.Count + " applications to check.");
                for (int i = 0; i < list.Count; i++)
                {
                    if (initial || (DateTime.Now - list[i].LastCheckTime).TotalHours >= 3)
                        this.PerformUpdateSingle(list[i].FilesystemPath);
                    else
                        this.m_DualLog.WriteEntry("The application at " + list[i].UpdateUrl + " has been checked in the last 3 hours.");
                }

                this.m_DualLog.WriteEntry("Periodic update check has finished.");
            }
            catch (Exception e)
            {
                this.m_DualLog.WriteException("Periodic update check failed with exception:", e);
            }
        }
开发者ID:hach-que,项目名称:Pivot.Update,代码行数:27,代码来源:Manager.cs

示例14: PerformUpdateSingle

        private void PerformUpdateSingle(string localPath, string restartPath = null)
        {
            Cache c = new Cache(false);
            c.Set<bool>("locked", true);
            ApplicationInstanceList list = c.Get<ApplicationInstanceList>("instances");
            for (int i = 0; i < list.Count; i++)
            {
                if (list[i].FilesystemPath == localPath)
                {
                    this.m_DualLog.WriteEntry("Now checking " + list[i].UpdateUrl + " for updates.");

                    // Perform update check.
                    string result = "";
                    Application a = new Application(c, new Uri(list[i].UpdateUrl), list[i].FilesystemPath);
                    list[i].LastCheckTime = DateTime.Now;
                    try
                    {
                        a.Update((status, info) =>
                        {
                            switch (status)
                            {
                                case Application.UpdateStatus.Starting:
                                    result += "Update is starting...\r\n";
                                    break;
                                case Application.UpdateStatus.RetrievingFileList:
                                    result += "Retrieving file list... ";
                                    break;
                                case Application.UpdateStatus.RetrievedFileList:
                                    result += "done (" + info + " files to scan).\r\n";
                                    break;
                                case Application.UpdateStatus.DeletionStart:
                                    result += "Deleting " + info + "... ";
                                    break;
                                case Application.UpdateStatus.DownloadNewStart:
                                case Application.UpdateStatus.DownloadFreshStart:
                                    result += "Downloading " + info + "... ";
                                    break;
                                case Application.UpdateStatus.PatchStart:
                                    result += "Patching " + info + "... ";
                                    break;
                                case Application.UpdateStatus.PatchApplied:
                                    result += info + ". ";
                                    break;
                                case Application.UpdateStatus.Complete:
                                    result += "Update is complete.\r\n";
                                    break;
                                case Application.UpdateStatus.DeletionFinish:
                                case Application.UpdateStatus.DownloadNewFinish:
                                case Application.UpdateStatus.DownloadFreshFinish:
                                case Application.UpdateStatus.PatchFinish:
                                    result += "done.\r\n";
                                    break;
                            }
                        });

                        this.m_DualLog.WriteEntry(list[i].UpdateUrl + " has been checked for updates.\r\n\r\n" + result);
                    }
                    catch (WebException)
                    {
                        this.m_DualLog.WriteEntry("Unable to check " + list[i].UpdateUrl + " for updates.  The server is not responding.", EventLogEntryType.Warning);
                    }
                }
            }
            c.Set<ApplicationInstanceList>("instances", list);
            c.Set<bool>("locked", false);

            // Restart game if possible.
            if (restartPath != null)
                WindowsNative.LaunchPathAsUser(restartPath);
        }
开发者ID:hach-que,项目名称:Pivot.Update,代码行数:70,代码来源:Manager.cs


注:本文中的Cache.Set方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。