Skip to content

diginsight/smartcache

Repository files navigation

INTRODUCTION

diginsight SmartCache provides hybrid, distributed, multilevel caching based on age sensitive data management.

  • SmartCache is hybrid as it caches data in-memory and on external RedIs databases.
    In-memory cache ensure 0-latency for most recently used data and ensures low pressure (and reduced cost) on the external RedIs database.
  • SmartCache is distributed as cache entries on different nodes of a multiinstance application are sinchronized automatically, to avoid flickering of values when querying the same data on different nodes.
  • SmartCache is based on age sensitive data management as cache entries are returned based on a requested MaxAge parameter.
    Data is returned from the cache if the requested MaxAge is compatible with the cache entry.
    Otherwise data is requested to the real data provider.
    This allows requesting data with different MaxAge criteria, according to the specific application condition.
    Data loaded by any request, is made available for the benefit of further requests (as long as compatible with their MaxAge requirement).

alt text

  • SmartCache is Multilevel: The same entries can be cached in multiple levels (frontend, backend or further levels).
    At any level, data is returned from the cache if the requested MaxAge is compatible with the cache entry. otherwise data is requested to the further levels.
    In case all levels entries contains old data, incompatible with the request MaxAge requirement, data is requested to the real data provider.

  • SmartCache is Optimized: as:

    • Privileges In-memory cache => it is faster as in memory cache hits are '0-Latency'
    • Minimizes use of external backing storage (e.g. RedIS) => it is cheaper and scalable as accesses to the backing storage are minimized
    • Replicas synchronize always keys and small values, bigger values are synchronized on demand

alt text

SmartCache supports data preloading and automatic invalidation of the cache entries so, data load latencies can be cut since the first call.

ADDITIONAL INFORMATION

SmartCache supports caching data with low cost and high performance.
In particular, 0 latency is ensured on in-memory cache hits. also, pressure on external RedIS resource is low as most frequently used entries are managed in-memory.

Also, 0 latency can be obtained since the first and for every call by means of Cache Preloading and Cache Invalidation.

Paragraph STEPS TO USE SMARTCACHE discusses basic steps to start using Diginsight.SmartCache.
The following articles discuss the details of Diginsight.SmartCache use and configuration:

STEPS TO USE SMARTCACHE

STEP 01: add a reference to Diginsight.SmartCache

In the first step you can just add a Diginsight.SmartCache reference to your code:
alt text

STEP 02: register SmartCache services into the startup sequence

SmartCache services and default settings must be registered into the startup sequence ConfigureServices methdod.
The code snippets below are available as working samples within the smartcache_samples repository.

public void ConfigureServices(IServiceCollection services)
{
    ...
    ...

    // configures SmartCache config section with default settings and support of Dynamic-Configuration for MaxAge, expirations etc
    services.Configure<SmartCacheCoreOptions>(configuration.GetSection("Diginsight:SmartCache"))
            .PostConfigureClassAwareFromHttpRequestHeaders<SmartCacheCoreOptions>();

    // adds smartCache services (ISmartCache, ICacheKeyService and other internal services)
    var smartCacheBuilder = services.AddSmartCache();

    IConfigurationSection smartCacheServiceBusConfiguration = configuration.GetSection("Diginsight:SmartCache:ServiceBus");
    if (!string.IsNullOrEmpty(smartCacheServiceBusConfiguration[nameof(SmartCacheServiceBusOptions.ConnectionString)]) &&
        !string.IsNullOrEmpty(smartCacheServiceBusConfiguration[nameof(SmartCacheServiceBusOptions.TopicName)]))
    {
        // (opt) registers ServiceBus companion for synchronization of SmartCache entries across application instances
        smartCacheBuilder
            .SetServiceBusCompanion(
                sbo =>
                {
                    smartCacheServiceBusConfiguration.Bind(sbo);
                    sbo.SubscriptionName = SmartCacheServiceBusSubscriptionName;
                }
            );
    }

}

the image below shows Diginsight.SmartCache settings with default MaxAge and Expiration values for cache entries.

alt text

NB. STEP02 only installs smartCache as an in-memory service.
An additional step can be added to install RedIS support to SmartCache distributed caching.

Diginsight.SmartCache will manage cache entries synchronization across application instances by means of the SetServiceBusCompanion.
HowTo: Configure SmartCache synchronization across application instances

STEP 03: load data by means of Diginsight.SmartCache

load your data by means of Diginsight.SmartCache cacheService

public async Task<SiteLicensesResponse> GetSiteLicensesAsync(string plantId, string plantType, ContextBase context)
{
    using var activity = DiginsightDefaults.ActivitySource.StartMethodActivity(logger, new { plantId, plantType });

    // define a key for the cache entry
    // NB. the cache key should include all imput parameters (that may cause different responses)
    // in this case the key is defined as a record including all relevant input parameters
    var cacheKey = new MethodCallCacheKey(cacheKeyService, typeof(PermissionServiceAdapter), nameof(GetSiteLicensesAsync), plantId, plantType);
    // data with max-age 10 minutes is requested
    var options = new SmartCacheOperationOptions() { MaxAge = TimeSpan.FromSeconds(600) }; 
    // load data by means of smartCache service
    // delegate to load real data from the back end must be passed to smartCache service
    var siteLicensesResponse = await smartCache.GetAsync(cacheKey,
        _ => GetSiteLicensesImplAsync(plantId, plantType, context), options
    );

    activity.SetOutput(siteLicensesResponse);
    return siteLicensesResponse;
}

STEP 04: Add RedIS companion for support of Distributed Hybrid cache

an additional companion can be installed to make

Diginsight.SmartCache will manage caching to in-memory cache or red-is cache based on cache entry size, retrieval latency etc.

For more information visit: SmartCache

Contribute

Contribute to the repository with your pull requests.

License

See the LICENSE file for license rights and limitations (MIT).

About

diginsight SmartCache provides hybrid, distributed, multilevel caching based on age sensitive data management

Topics

Resources

License

MIT, MIT licenses found

Licenses found

MIT
LICENSE
MIT
LICENSE.md

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published