{"id":21908654,"url":"https://github.com/vb2ae/aspnetcorecaching","last_synced_at":"2025-03-22T07:47:22.684Z","repository":{"id":70937735,"uuid":"126742701","full_name":"vb2ae/ASPNETCORECACHING","owner":"vb2ae","description":"Sample app which shows how to use caching in asp.net core 2.0","archived":false,"fork":false,"pushed_at":"2023-11-04T13:58:12.000Z","size":534,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-01-27T08:11:35.473Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"HTML","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"unlicense","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/vb2ae.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-03-25T21:41:01.000Z","updated_at":"2024-12-18T20:49:19.000Z","dependencies_parsed_at":"2023-11-04T14:33:27.185Z","dependency_job_id":null,"html_url":"https://github.com/vb2ae/ASPNETCORECACHING","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vb2ae%2FASPNETCORECACHING","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vb2ae%2FASPNETCORECACHING/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vb2ae%2FASPNETCORECACHING/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vb2ae%2FASPNETCORECACHING/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/vb2ae","download_url":"https://codeload.github.com/vb2ae/ASPNETCORECACHING/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244924752,"owners_count":20532873,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-28T17:13:11.699Z","updated_at":"2025-03-22T07:47:22.666Z","avatar_url":"https://github.com/vb2ae.png","language":"HTML","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Asp.net Core Caching\n\nSample app which shows how to use caching in asp.net core 2.0\n\n\n\nWith classic Asp.Net you used the HtppContext in the System.Web namespace for caching. Since Asp.Net Core was created to be cross platform caching is done differently.\n\nOnce the app is created go to the startup.cs class. and change the ConfigureServices method to this\n\n        public void ConfigureServices(IServiceCollection services)\n        {\n            services.AddMvc();\n            services.AddMemoryCache();\n        }  \n\n\nNow to use this we need to get access to the IMemoryCache in the controllers constructor.  In the demo I will use the about page in the project created\n\n        IMemoryCache caching= null;\n\n\n        public HomeController(IMemoryCache cache)\n        {\n            caching = cache;\n        }\n\nTo use it. I am just going to cache the DateTime\n\n        public IActionResult About()\n        {\n            ViewData[\"Message\"] = \"Your application description page.\";\n            DateTime currentTime;\n            if (!caching.TryGetValue\u003cDateTime\u003e(\"currentTime\", out currentTime))\n            {\n                currentTime = DateTime.Now;\n                caching.Set\u003cDateTime\u003e(\"currentTime\", currentTime, DateTimeOffset.Now.AddMinutes(10));\n            }\n        \n                \n            ViewBag.Time = currentTime;\n\n            return View();\n        }\n\nTo cache something use the Set method.  I would recommend using the version that allow you to specify the type of object.\n\nWhen setting the cache you pass in the key, what you want cached and optionally how long you want it cached.  I set it to 10 minutes in this example.\n\nGetting items from the cache you use TryGetValue it will return true if the value was found and it passes it out in an out parameter.\n\nIf you need to remove something use the cache Remove method where you pass in the key of the object you want to remove.\n\nTo show the item is cached I changed the About.cshtml to show the current date time and the cached value\n\n@{\n    ViewData[\"Title\"] = \"About\";\n}\n\u003ch2\u003e@ViewData[\"Title\"]\u003c/h2\u003e\n\u003ch3\u003e@ViewData[\"Message\"]\u003c/h3\u003e\n\n\u003cp\u003eUse this area to provide additional information.\u003c/p\u003e\n@DateTime.Now\n\u003cbr/\u003e\n@ViewBag.Time\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvb2ae%2Faspnetcorecaching","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fvb2ae%2Faspnetcorecaching","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvb2ae%2Faspnetcorecaching/lists"}