Giter VIP home page Giter VIP logo

csf.caches's Introduction

Archived: Use MS Extensions Caching instead

This library is archived and no longer maintained. The reason is that Microsoft have released Microsoft.Extensions.Caching.Abstractions which provides far superior support and a very similar API for caching objects in a simple manner. In particular, former users of this library should consider migrating to Microsoft.Extensions.Caching.Memory.IMemoryCache.


Typed cache adapter

This package provides ICachesObjects<TKey,TValue>, which is a type-safe cache implementation. The implementation TypedObjectCacheAdapter<TKey,TValue> is an adapter for instances of System.Runtime.Caching.ObjectCache.

The primary motivations for this cache interface are:

  • Type safety
  • Simplification of the API exposed to consumers

The simplification is achieved by configuring the caching policy and storage regions from the constructor, rather than requiring consumers to pass this information directly.

Namespaced-region cache decorator

The class NamespacedRegionCacheDecorator is decorator for System.Runtime.Caching.ObjectCache which may be used to add region support to ObjectCache implementations that do not otherwise support them. This is achieved cheaply by prepending namespaced strings to the cache keys in the underlying implementation. The namespace identifiers make use of Guid keys, making them very difficult to predict and thus reasonably safe from key-collision attacks. Please see the remarks upon the class itself for more information.

TypedObjectCacheAdapter usage

In your application, you should have one or more instances of ObjectCache, which are used as the 'backing stores' for the data you wish to cache. For each object type you wish to cache (key/value pairs), create a single instance of TypedObjectCacheAdapter<TKey,TValue>, wrapping the appropriate object cache.

Constructing & sharing instances

When constructing the cache adapter, you may provide a callback which generates a CacheItemPolicy for items which are added to the cache. You may also optionally choose a cache region, assuming the underlying object cache implementation supports it.

A single instance of the cache adapter should have a long-lifetime, usually for the whole lifetime of the application. Ideally, use dependency injection to share a single instance of the cache adapter between any types which need to consume it.

Cache key types

Because the underlying ObjectCache class requires string keys to store instances, any class used as a cache key, must implement the interface IGetsCacheKey.

Further documentation

The API for the cache is documented in the interface ICachesObjects<TKey,TValue>.

NamespacedRegionCacheDecorator usage

To use this class, simply construct it wrapping an existing object cache instance. You should either keep this object as a singleton within your application or you must ensure that you manually construct and keep the accompanying instance of IGetsRegionBasedCacheKeys (for example NamespacedRegionCacheKeyProvider). This key-provider object maintains the map of region names to their Guid keys. If that is lost, then the cached items will all become unreachable, because their true keys in the underlying cache will be lost.

Open source license

All source files within this project are released as open source software, under the terms of the MIT license.

csf.caches's People

Contributors

craigfowler avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.