GithubHelp home page GithubHelp logo

truelime.kentico.fluentcaching's Introduction

TrueLime.Kentico.FluentCaching

Fluent API for caching data with Kentico Data API

Introduction

This library was born out of frustration about manually formatting cache keys and cache dependency keys for Kentico. When you're writing a bunch of data access code, for example for a Kentico MVC application, it becomes very tedious and error prone to hand code all the cache dependency keys.

We'd end up with these complex data access methods and then a blurb cache configuration code that looks cryptic at best.

The Kentico CacheHelper offers a more semantic interface on top of the Kentico Cache helpers. Looking the blurb of cache configuration is still there, but it's much easier to write, read and maintain.

Requirements

This code has been tested and is currently used in production on multiple projects running:

  • Kentico 9 to 13
  • MVC or Portal Engine

Installation instructions

Install the nuget package into the project that holds your custom code. This can be the website project or a class library project used in the CMS or and MVC site.

    Install-Package TrueLime.Kentico.FluentCaching.Sources

Note that this is a source code package. It will add source code to your project but will not add additional assembly dependencies. This way we hope to prevent clashes between different versions of this code in different assemblies and keep this functionality neetly contained within your assembly.

Example - generic data access

The default implementation takes culture, site name and domain name of the current request from the (Kentico) context.

  FluentCacheHelper.GetCachedResult(() =>
        // Data access goes here
            DocumentHelper.GetDocuments<NewsItem>()
                .LatestVersion(false)
                .Published(true)
                .OnCurrentSite()
                .Culture(cultureName)
                .OrderByIfNullColumn("DocumentPublishFrom", "DocumentCreatedWhen", OrderDirection.Descending)
                .Page(pageCount, pageSize);
                .ToArray(),
        
        // Cache configuration goes here
            CacheThis.As("latestnews")
                .ForMinutes(10)
                .WithParameter("count", pageCount)
                .WithParameter("size", pageSize)
                .DependsOnPageType(NewsItem.CLASS_NAME));

Example - MVC repository style

In an MVC repository, you'd probably use the pattern set out in the Kentico MVC sample application. This includes support for setting the culture and site name per repository, as well as enabling support for previewing content.

On the base repository, you'd have something like this:

public abstract class CachingRepositoryBase {
    public CachingRepositoryBase( string siteName, string cultureName, bool enableCaching ){

    }

    public CacheThis CacheAs(string key)
    {
        return new CacheThis(_cultureName, _siteName, key);
    }

    protected TResult GetCachedResult<TResult>(Func<TResult> provideData, CacheThis settings)
    {
         return FluentCacheHelper.GetCachedResult(provideData, settings, _enableCaching);
    }
}

Then in the repository implementation you can do this:

public class ContentPageRepository : CachingRepositoryBase, IContentPageRepository
{

    // .... constructor etc here ...

    public ContentPage GetContentPage(int nodeId)
    {
        return GetCachedResult(() =>
        // Data access goes here
        DocumentHelper.GetDocuments<ContentPage>()
            .ByNodeId(nodeId)
            .AddSpecificColumns()
            .AddDefaultClauses(_latestVersionEnabled, _cultureName)
            .WhereEqualsOrNull(nameof(CMS.DocumentEngine.TreeNode.NodeLinkedNodeID), 0),
        
        // Cache configuration goes here
        CacheAs(ContentPage.CLASS_NAME)
                    .ForMinutes(5)
                    .WithParameter("nodeId", nodeId)
                    .DependsOnNode(nodeId)
            );
    }
}

truelime.kentico.fluentcaching's People

Contributors

jeroenfurst avatar alanta avatar sietset avatar

Stargazers

Dennis Hulsmans avatar Kristian Bortnik avatar amir avatar

Watchers

 avatar James Cloos avatar Dennis Hulsmans avatar  avatar  avatar Hans van der Linden 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.