{"id":19100715,"url":"https://github.com/Open-NET-Libraries/Open.Caching","last_synced_at":"2025-04-18T18:30:55.522Z","repository":{"id":43341270,"uuid":"105818486","full_name":"Open-NET-Libraries/Open.Caching","owner":"Open-NET-Libraries","description":"Useful set of utilites and extensions for simplifying cache usage.","archived":false,"fork":false,"pushed_at":"2023-04-23T02:43:04.000Z","size":196,"stargazers_count":12,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-11-03T18:08:55.222Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/Open-NET-Libraries.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}},"created_at":"2017-10-04T21:07:36.000Z","updated_at":"2024-05-03T23:05:45.000Z","dependencies_parsed_at":"2022-08-28T10:01:57.817Z","dependency_job_id":null,"html_url":"https://github.com/Open-NET-Libraries/Open.Caching","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/Open-NET-Libraries%2FOpen.Caching","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Open-NET-Libraries%2FOpen.Caching/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Open-NET-Libraries%2FOpen.Caching/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Open-NET-Libraries%2FOpen.Caching/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Open-NET-Libraries","download_url":"https://codeload.github.com/Open-NET-Libraries/Open.Caching/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":222880682,"owners_count":17051936,"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-09T03:52:57.688Z","updated_at":"2025-04-18T18:30:55.512Z","avatar_url":"https://github.com/Open-NET-Libraries.png","language":"C#","funding_links":[],"categories":["others"],"sub_categories":[],"readme":"# Open.Caching\n\n[![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg?style=flat-square)](https://www.nuget.org/packages/Open.Caching/blob/master/LICENSE)\n![100% code coverage](https://img.shields.io/badge/coverage-100%25-green)\n\nUseful set of DI/IoC agnostic interfaces, utilities and extensions for simplifying cache usage.\n\n## Implementation\n\nWith the following libraries, you can build other libraries that sever their dependency from any cache and allow you to inject whichever you want.\n\n---\n\n## Core Interfaces \u0026 Extensions\n\n### Open.Caching\n\n[https://www.nuget.org/packages/Open.Caching](https://www.nuget.org/packages/Open.Caching/)  \n[![NuGet](https://img.shields.io/nuget/v/Open.Caching.svg)](https://www.nuget.org/packages/Open.Caching/) Core package for interfaces and base classes.\n\n## Library/Vendor Specific Implementations\n\n### Open.Caching.Memory\n\n[https://www.nuget.org/packages/Open.Caching.Memory](https://www.nuget.org/packages/Open.Caching.Memory/)  \n[![NuGet](https://img.shields.io/nuget/v/Open.Caching.Memory.svg)](https://www.nuget.org/packages/Open.Caching.Memory/) Contains `MemoryCacheAdapter` for use with any `IMemoryCache`.\n\n### Open.Caching.Runtime\n\n[https://www.nuget.org/packages/Open.Caching.Runtime](https://www.nuget.org/packages/Open.Caching.Runtime/)  \n[![NuGet](https://img.shields.io/nuget/v/Open.Caching.Runtime.svg)](https://www.nuget.org/packages/Open.Caching.Runtime/) Contains `ObjectCacheAdapter` for use with any `System.Runtime.Caching.ObjectCache`.\n\n### Open.Caching.Web\n\n[https://www.nuget.org/packages/Open.Caching.Web](https://www.nuget.org/packages/Open.Caching.Web/)  \n[![NuGet](https://img.shields.io/nuget/v/Open.Caching.Web.svg)](https://www.nuget.org/packages/Open.Caching.Web/) Contains `WebCacheAdapter` for use with any `System.Web.Caching.Cache`.  \nUseful when attempting to transition code away from legacy ASP.NET.\n\n---\n\n## Notable Similarities \u0026amp; Differences\n\nThe above adapters all accept strings as keys,\nbut only `MemoryCacheAdapter` will accept any type of key as `IMemoryCache` uses `object`s as keys.  If your dependency injection configuration uses `ICacheAdapter\u003cstring\u003e` as its cache interface then any of the implementations can be used.  So if you are transitioning from a legacy ASP.NET environment, switching to `MemoryCacheAdapter\u003cstring\u003e` will make things easy.\n\nEvery cache implementation listed handles absolute and sliding expiration.\n\nBecause `IMemoryCache` allows for `null` values to be inserted,\nthe other implementations use a placeholder `NullValue` to indicate `null` and retain parity for all implementations.\n\n## Not Yet Supported\n\nSome of the reasons for not supporting certain features should be obvious.  \nThe intention of these utilities is to cover the 95%+ use case.  \nSetting expiration is very common, but setting priority is not so common.\n\n* At this time, 'priority' is not supported as each cache has a slightly different implementation.\n* Eviction call backs, cache item or file system watchers.\n\n---\n\n## Interfaces, Classes, \u0026amp; Structs\n\n### `ICacheAdapter\u003cTKey\u003e` \n\nModeled after `Microsoft.Extensions.Caching.Memory.IMemoryCache`, this interface facilitates cache access for all adapters and extensions.\n\n```cs\nnamespace Open.Caching;\n\npublic interface ICacheAdapter\u003cTKey\u003e\n{\n\tbool TryGetValue\u003cTValue\u003e(\n\t\tTKey key,\n\t\tout TValue item,\n\t\tbool throwIfUnexpectedType = false);\n\n\tvoid Set\u003cTValue\u003e(TKey key, TValue item);\n\n\tvoid Remove(TKey key);\n}\n```\n\nIt does not offer a mechanism for a cache policy as that is provided by `CacheAdapterBase\u003cTKey, TCache\u003e`.\n\n### `ExpirationPolicy`\n\nThis read only struct combines both `.Absolute` and `.Sliding` expirations into `TimeSpan` values.\n\n`.Absolute` is a `TimeSpan` as it is almost always the case that expiration happens relative from when the cache entry was inserted.\n\n`DateTimeOffset AbsoluteRelativeToNow` is derived from the value of `.Absolute` and the `DateTimeOffset.Now`\n\n### `ICachePolicyProvider\u003cTKey, TPolicy\u003e`\n\nThis interface allows for returning a specific `ICacheAdapter\u003cTKey` that will default to that policy.\n\n### `CacheAdapterBase\u003cTKey, TCache\u003e`\n\nEvery adapter derives from this base class and implements the `ICachePolicyProvider\u003cTKey, ExpirationPolicy\u003e` interface. Allowing for simple or policy driven cache access.\n\n---\n\n### `CacheItem\u003cTKey, TValue\u003e`\n\nThe intention of this and the following classes is to simplify access to a cached resource.  \nMuch like a `Lazy\u003cT\u003e`, or any other container class, you can affix, or pass around these classes without the consumer having to know what the key is.\n\n```cs\npublic class MyClass {\n\n    // Injected ICacheAdapter\u003cstring\u003e.\n    public MyClass(ICacheAdapter\u003cstring\u003e cache)\n    {\n        // The key is defined in only one place.\n        _value = cache\n            .CreateItem(\n                key: \"a cache key\",\n                defaultValue: \"[not set]\");\n    }\n\n    readonly CacheItem\u003cstring, string\u003e _value;\n    public string Value {\n        get =\u003e _value; // Implicit\n        set =\u003e _value.Value = value;\n    }\n}\n```\n\n### `LazyCacheItem\u003cTKey, TValue\u003e`\n\nThe important idea here is to allow for the insertion of a `Lazy\u003cT\u003e` so that any subsequent requests to that resource either wait for it to complete, or receive the already resolved value.\n\nThe underlying `.GetOrCreateLazy\u003cT\u003e` extension properly evicts the `Lazy\u003cT\u003e` if the `Value` property throws an exception.\n\n```cs\npublic class MyClass {\n\n    // Injected ICacheAdapter\u003cstring\u003e.\n    public MyClass(ICacheAdapter\u003cstring\u003e cache)\n    {\n        // The key is defined in only one place.\n        _value = cache\n            .CreateLazyItem(\n                key: \"a cache key\",\n                ()=\u003e{\n                /* long running process. */\n                });\n    }\n\n    public string Value =\u003e _value; // Implicit\n}\n```\n\n\n### `AsyncLazyCacheItem\u003cTKey, TValue\u003e`\n\nThis class implements `IAsyncCacheItem\u003cTValue\u003e` and therefore is awaitable.\n\nSimilar to the above, the underlying `.GetOrCreateLazyAsync` method uses a `Lazy\u003cTask\u003cT\u003e\u003e\u003e` to initialize the method and asynchronously produce a result.  Any exceptions thrown by the the `Task\u003cT\u003e` or its factory method will evict the entry from the cache.\n\n```cs\npublic class MyClass {\n\n    // Injected ICacheAdapter\u003cstring\u003e.\n    public MyClass(ICacheAdapter\u003cstring\u003e cache)\n    {\n        // The key is defined in only one place.\n        _value = cache\n            .CreateAsyncLazyItem(\n                key: \"a cache key\",\n                async ()=\u003e{\n                /* long running async process. */\n                });\n    }\n\n    public Task\u003cstring\u003e Value =\u003e _value; // Implicit\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FOpen-NET-Libraries%2FOpen.Caching","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FOpen-NET-Libraries%2FOpen.Caching","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FOpen-NET-Libraries%2FOpen.Caching/lists"}