{"id":22854878,"url":"https://github.com/datarza/system.web.caching.datacache","last_synced_at":"2026-05-15T20:06:13.560Z","repository":{"id":144199080,"uuid":"202915815","full_name":"datarza/System.Web.Caching.DataCache","owner":"datarza","description":"An improved cache for the ASP.NET applications","archived":false,"fork":false,"pushed_at":"2019-08-24T17:26:32.000Z","size":229,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-08-01T03:35:55.176Z","etag":null,"topics":["asp-net","aspnet","cache","cache-control","cache-storage","cachemanager","caching","caching-library","csharp","webapp","webcomponents","webforms","webforms-pages"],"latest_commit_sha":null,"homepage":"https://datarza.github.io/System.Web.Caching.DataCache/","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/datarza.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"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":"2019-08-17T18:02:27.000Z","updated_at":"2020-05-24T20:50:00.000Z","dependencies_parsed_at":"2023-06-16T21:15:31.657Z","dependency_job_id":null,"html_url":"https://github.com/datarza/System.Web.Caching.DataCache","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/datarza/System.Web.Caching.DataCache","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/datarza%2FSystem.Web.Caching.DataCache","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/datarza%2FSystem.Web.Caching.DataCache/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/datarza%2FSystem.Web.Caching.DataCache/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/datarza%2FSystem.Web.Caching.DataCache/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/datarza","download_url":"https://codeload.github.com/datarza/System.Web.Caching.DataCache/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/datarza%2FSystem.Web.Caching.DataCache/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":33078052,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-15T20:05:40.333Z","status":"ssl_error","status_checked_at":"2026-05-15T20:05:38.672Z","response_time":103,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":["asp-net","aspnet","cache","cache-control","cache-storage","cachemanager","caching","caching-library","csharp","webapp","webcomponents","webforms","webforms-pages"],"created_at":"2024-12-13T07:08:55.695Z","updated_at":"2026-05-15T20:06:13.528Z","avatar_url":"https://github.com/datarza.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"The ASP.NET WebForms Cache was implemented very conveniently for temporary keeping the operational data. In the early years of the .NET platform, developers often used to work with `System.Web` namespace, even in WinForms applications.\n\nThe code template, that is constantly being offered in articles about ASP.NET caching, is very simple and practical:\n\n```csharp\n// try to get an instance of object from cache\nDataSet ds = HttpRuntime.Cache[\"KeyName\"] as DataSet;\n// check the result and recreate it, if it is null\nif (ds == null)\n{\n    ds = QueryDataFromDatabase();\n    HttpRuntime.Cache.Insert(\"KeyName\", ds);\n}\n// using the instance of object that has been populated from cache or from storage\nDataRow dr = ds.Tables[0].Rows[0];\n```\n\nHowever, the ASP.NET Cache implementation does not include some desirable features.\n\n### ASP.NET cache settings\n\nAlthough the [Cache.Insert(String, Object)](https://docs.microsoft.com/en-us/dotnet/api/system.web.caching.cache.insert?view=netframework-1.1#System_Web_Caching_Cache_Insert_System_String_System_Object_) method, that adds a value into the cache, is suitable for most cases, it is often desirable to applay different settings for caching data based on the web application's settings stored in the Web.config file, and sometimes even completely disabling the cache for the entire application. In this case, the source code should not be changed and application should run without recompiling.\n\n### Generic methods and default values\n\nThe ASP.NET cache always returns instances of the `object` class, no matter what the actual type of the instance is. In most cases, this is not a problem, because the `Nullable types` can be used instead of the `Value types`. Perhaps you would like to have generic methods for retrieving data from the cache:\n\n```csharp\n// this is a dafault extraction data from cache\nmyClassName item = HttpRuntime.Cache[\"Key1\"] as myClassName; \n\n// this is a desired extraction data with generic methods\nmyClassName item = DataCache.Get\u003cmyClassName\u003e(\"Key1\"); \n```\n\nIf the generic methods are not satisfied for you, then it is possible to use the default version. In fact, these two options are identical. But the generic methods offer additional features. For example, the default value could be define if the cache does not contains a value to be retrieved:\n\n```csharp\nmyClassName defaultValue = new myClassName(/* init properties */);\nmyClassName item = DataCache.Get\u003cmyClassName\u003e(\"Key1\", defaultValue); \n/* if there is nothing in the cache, then the item will be defaultValue */\n```\n\n### Deep copied data\n\nThe ASP.NET cache always returns the object stored in the cache. This means that changes to any property of the extracted object will also change the object stored in the cache, since these are the same objects. In some cases, you may want to modify the retrieved object, but leave the object in the cache unchanged. One of the convenient functionality is to retrieve a copy of an object from the cache which can be changed without worrying about the object in the cache.\n\n### Regions of cheched data \n\nThe ASP.NET cache is similar to a `NameValueCollection`. It is simple and elegant solution for a small web application, but it becomes difficult to generate the keys when application grows up. For example, consider caching two related tables, such as Vendors and Models. In this case web application will keep all data from the Vendors table in one cached object and many cached objects for associated records of the Models table. The key for Vendors supposed to be a `Vendors` and the keys for Models supposed to be `Models.[VendorID]`. When Vendors table has changed, cached instance for Vendors and only associated instances for Models have to be removed from cache. This means that data should be cached and removed from cache by regions.\n\n## Implementation\n\nThe described functions are optional and can be easily implemented in any web application. [Proxy template](https://en.wikipedia.org/wiki/Proxy_pattern) is a good option for implementing all the described features. The resulted library can be used on all .NET Frameworks, starting version 2.0. The .NET Framework 4.0 introduces the `System.Runtime.Caching` namespace and several classes with a new caching model. Therefore, using the standard ASP.NET cache and its improvements is not required.\n\nSad but true.\n\n### Properties and settings\n\n```csharp\n/// \u003csummary\u003e\n/// Is cache used or not\n/// \u003c/summary\u003e\npublic static bool IsCacheEnable;\n\n/// \u003csummary\u003e\n/// How to store objects in the cache\n/// \u003c/summary\u003e\npublic static CacheExpirationType ExpirationType;\n\n/// \u003csummary\u003e\n/// How long objects should be stored in the cache \n/// \u003c/summary\u003e\npublic static TimeSpan ExpirationTime;\n```\n\nEnum `CacheExpirationType` defines how to store objects in the cache\n\n```csharp\n/// \u003csummary\u003e\n/// How to store objects in the cache\n/// \u003c/summary\u003e\npublic enum CacheExpirationType\n{\n    /// \u003csummary\u003e\n    /// Without time limit, the value of the \u003cseealso cref = \"DataCache.ExpirationTime\" /\u003e property will be ignored\n    /// \u003c/summary\u003e\n    NoExpiration,\n\n    /// \u003csummary\u003e\n    /// The \u003cseealso cref=\"DataCache.ExpirationTime\"/\u003e at which the inserted object expires and is removed from the cache\n    /// \u003c/summary\u003e\n    AbsoluteExpiration,\n\n    /// \u003csummary\u003e\n    /// The interval \u003cseealso cref=\"DataCache.ExpirationTime\"/\u003e between the time the inserted object is last accessed and the time at which that object expires\n    /// \u003c/summary\u003e\n    SlidingExpiration\n}\n```\n\n### Methods to extract data from the cache\n\n```csharp\n/// \u003csummary\u003e\n/// Retrieves the specified item from the Cache object\n/// \u003c/summary\u003e\n/// \u003ctypeparam name=\"T\"\u003eThe type for the cache item to retrieve\u003c/typeparam\u003e\n/// \u003cparam name=\"key\"\u003eThe identifier for the cache item to retrieve\u003c/param\u003e\n/// \u003cparam name=\"defaultValue\"\u003eThe default value if the object is not in the cache\u003c/param\u003e\n/// \u003creturns\u003eThe retrieved cache item, or \u003cparamref name=\"defaultValue\"/\u003e if the key is not found\u003c/returns\u003e\npublic static T GetData\u003cT\u003e(string key, T defaultValue = default(T))\n\n/// \u003csummary\u003e\n/// Retrieves the deep copied of specified item from the Cache object\n/// \u003c/summary\u003e\n/// \u003ctypeparam name=\"T\"\u003eThe type for the cache item to retrieve\u003c/typeparam\u003e\n/// \u003cparam name=\"key\"\u003eThe identifier for the cache item to retrieve\u003c/param\u003e\n/// \u003cparam name=\"defaultValue\"\u003eThe default value if the object is not in the cache\u003c/param\u003e\n/// \u003creturns\u003eThe retrieved cache item, or \u003cparamref name=\"defaultValue\"/\u003e if the key is not found\u003c/returns\u003e\npublic static T GetDeepCopiedData\u003cT\u003e(string key, T defaultValue = default(T))\n```\n\n### Methods to store data in the cache\n\n```csharp\n/// \u003csummary\u003e\n/// Inserts an item into the cache with a cache key to reference its location, using default values provided by the settings\n/// \u003c/summary\u003e\n/// \u003cparam name=\"key\"\u003eThe cache key used to reference the item\u003c/param\u003e\n/// \u003cparam name=\"value\"\u003eThe object to be inserted into the cache\u003c/param\u003e\npublic static void InsertData(string key, object value)\n\n/// \u003csummary\u003e\n/// Inserts an item into the cache with a cache key to reference its location, using the absolute expiration time\n/// \u003c/summary\u003e\n/// \u003cparam name=\"key\"\u003eThe cache key used to reference the item\u003c/param\u003e\n/// \u003cparam name=\"value\"\u003eThe object to be inserted into the cache\u003c/param\u003e\n/// \u003cparam name=\"expirationTime\"\u003eHow long the object should be stored in the cache\u003c/param\u003e\npublic static void InsertAbsoluteExpirationData(string key, object value, TimeSpan expirationTime)\n\n/// \u003csummary\u003e\n/// Inserts an item into the cache with a cache key to reference its location, using the sliding expiration time\n/// \u003c/summary\u003e\n/// \u003cparam name=\"key\"\u003eThe cache key used to reference the item\u003c/param\u003e\n/// \u003cparam name=\"value\"\u003eThe object to be inserted into the cache\u003c/param\u003e\n/// \u003cparam name=\"expirationTime\"\u003eHow long the object should be stored in the cache\u003c/param\u003e\npublic static void InsertSlidingExpirationData(string key, object value, TimeSpan expirationTime)\n\n/// \u003csummary\u003e\n/// Inserts an item into the cache with a cache key to reference its location, using the type of expiration and default value for expiration time\n/// \u003c/summary\u003e\n/// \u003cparam name=\"key\"\u003eThe cache key used to reference the item\u003c/param\u003e\n/// \u003cparam name=\"value\"\u003eThe object to be inserted into the cache\u003c/param\u003e\n/// \u003cparam name=\"expirationType\"\u003eHow to store objects in the cache\u003c/param\u003e\npublic static void InsertExpirationData(string key, object value, CacheExpirationType expirationType)\n\n/// \u003csummary\u003e\n/// Inserts an item into the cache with a cache key to reference its location, using the type of expiration and expiration time\n/// \u003c/summary\u003e\n/// \u003cparam name=\"key\"\u003eThe cache key used to reference the item\u003c/param\u003e\n/// \u003cparam name=\"value\"\u003eThe object to be inserted into the cache\u003c/param\u003e\n/// \u003cparam name=\"expirationType\"\u003eHow to store objects in the cache\u003c/param\u003e\n/// \u003cparam name=\"expirationTime\"\u003eHow long the object should be stored in the cache\u003c/param\u003e\npublic static void InsertExpirationData(string key, object value, CacheExpirationType expirationType, TimeSpan expirationTime)\n```\n\n### Methods to remove data from the cache\n\n```csharp\n/// \u003csummary\u003e\n/// Removes the specified item from the application's cache \n/// \u003c/summary\u003e\n/// \u003cparam name=\"key\"\u003eAn identifier for the cache item to remove\u003c/param\u003e\npublic static void RemoveDataByKey(string key)\n\n/// \u003csummary\u003e\n/// Removes all items from the application's cache that starts with key\n/// \u003c/summary\u003e\n/// \u003cparam name=\"keyStartsWith\"\u003eAn starts with identifier for the cache item to remove\u003c/param\u003e\npublic static void RemoveAllDataByKey(string keyStartsWith)\n\n/// \u003csummary\u003e\n/// Removes all items from the application's cache \n/// \u003c/summary\u003e\npublic static void RemoveAllData()\n```\n\n## Usage example\n\n#### 1. Settings the default parameters when application starts\n\n```csharp\npublic class Global : System.Web.HttpApplication\n{\n    protected void Application_Start(object sender, EventArgs e)\n    {\n        Settings settings = Settings.Default;\n        DataCache.IsCacheEnable = settings.IsCacheEnable;\n        DataCache.ExpirationType = settings.ExpirationType;\n        DataCache.ExpirationTime = settings.ExpirationTime;\n    }\n}\n```\n\n#### 2. Implement the standard template working with cache on the Page or in DAL\n\n```csharp\npublic partial class DefaultPage : System.Web.UI.Page\n{\n    public const string STR_CACHENAME = \"something\";\n\n    protected void Page_Load(object sender, EventArgs e)\n    {\n        if (!this.IsPostBack)\n        {\n            SomethingDataModel val = DataCache.GetData\u003cSomethingDataModel\u003e(STR_CACHENAME);\n            if (val == null) \n            {\n                SomethingDataModel val = new SomethingDataModel(); // get data from ...\n                DataCache.InsertData(STR_CACHENAME, val);\n            }\n            DataBind(val); // use data on the page\n        }\n    }\n}\n```\n\nIf you want to define time settings that are different from specified by default in the cache, use one of the overridden `Insert{Which}Data` methods.\n\n#### 3. In the admin panel or when the database was updated remove also cached values by key or by key that starts with\n\n```csharp\npublic partial class DefaultPage : System.Web.UI.Page\n{\n    public const string STR_CACHENAME = \"something\";\n\n    protected void Page_Load(object sender, EventArgs e)\n    {\n        if (this.IsPostBack)\n        {\n            DAL.Update(); // update the database ...\n            DataCache.RemoveAllDataByKey(STR_CACHENAME); // clear cache\n            Response.Redirect(\"Default.aspx\"); // proccessing the request\n        }\n    }\n}\n```\n\n## Support or Contact\n\nHaving questions? [Contact me](https://github.com/CanadianBeaver) and I will help you sort it out.\n\n\u003cstyle\u003e.inner { min-width: 800px !important; max-width: 60% !important;}\u003c/style\u003e\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdatarza%2Fsystem.web.caching.datacache","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdatarza%2Fsystem.web.caching.datacache","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdatarza%2Fsystem.web.caching.datacache/lists"}