• Facebook
  • Twitter
  • Youtube
  • LinkedIn
  • RSS
  • Docs
  • Comparisons
  • Blogs
  • Try NCache
  • Contact Us
Try NCache
Show / Hide Table of Contents

Object Caching Using Cache API

Object Caching is used to cache application data such as the result of a database query in a distributed environment to minimize trips to the database.

ASP.NET Core applications can use NCache for object caching using the NCache Cache API. Here we describe how to get started with NCache Cache API in an ASP.NET Core application. You can see Client-side API Programming for a complete list of NCache-provided distributed caching features.

Prerequisites

Before using this, ensure that the following prerequisites are fulfilled:

  • .NET
  • Install the following NuGet packages in your application:
    • NCache: Alachisoft.NCache.SDK
  • The cache must be running.
  • The application must be connected to cache before performing the operation.
  • For API details, refer to: ICache, CacheManager, GetCache, Get, CacheItemExpiration, ExpirationType, Insert
  • Make sure that the data being added is serializable.

Using NCache for Object Caching in ASP.NET Core

You can connect to an instance of NCache within the application using the GetCache API and proceed to perform object caching operations.

Note

To utilize the NCache APIs for object caching, include the following namespace in your application: Alachisoft.NCache.Client.

The following code initializes a cache and tries to fetch an album with a given ID from the cache. If not found in the cache, it fetches the specified data from the database and inserts it into the cache with a Sliding Expiration of 10 minutes.

public async Task<IActionResult> Details(int id)
{
   ICache _cache = CacheManager.GetCache(_appSettings.CacheName); // Connect to cache in NCache

    var cacheKey = string.Format("album_{0}", id);

    Album album = null;

    if (_cache != null)
    {
        album = _cache.Get<Album>(cacheKey); // Fetch Album object
    }
    if (album == null)
    {
        album = await DbContext.Albums
                        .Where(a => a.AlbumId == id)
                        .Include(a => a.Artist)
                        .Include(a => a.Genre)
                        .FirstOrDefaultAsync();

        if (album != null)
        {
            if (_appSettings.CacheDbResults && _cache != null)
            {           
                var cacheItem = new CacheItem(album);
                cacheItem.SlidingExpiration = TimeSpan.FromMinutes(10);

                // Add CacheItem to cache
                _cache.Insert(cacheKey, cacheItem);
            }
        }
    }
    return View(album);
}
Note

To ensure the operation is fail-safe, it is recommended to handle any potential exceptions within your application, as explained in Handling Failures.

See Also

.NET: Alachisoft.NCache.Caching.Distributed namespace.

Contact Us

PHONE

+1 214-619-2601   (US)

+44 20 7993 8327   (UK)

 
EMAIL

sales@alachisoft.com

support@alachisoft.com

NCache
  • Edition Comparison
  • NCache Architecture
  • Benchmarks
Try NCache
Pricing
NCache Playground

Deployments
  • Cloud
  • Docker
  • Kubernetes
  • On-Premises
Technical Use Cases
  • ASP.NET Sessions
  • ASP.NET Core Sessions
  • Pub/Sub Messaging
  • SignalR Backplane
  • Internet of Things (IoT)
  • NoSQL Database
  • Stream Processing
  • Microservices
Resources
  • Blogs
  • Foundations
  • Videos
  • Comparisons
  • Shows
  • Whitepapers
  • Articles
  • Tech Talks
  • Magazine Articles
  • Third-Party Articles
Customer Case Studies
  • Testimonials
  • Customers
Support
  • Docs
  • Schedule a Demo
  • Tips
Company
  • Leadership
  • Partners
  • News
  • Events
  • Careers
Contact Us

  • EnglishChinese (Simplified)FrenchGermanItalianJapaneseKoreanPortugueseSpanish

  • Terms of Use
  •  
  • Privacy Policy
  •  
  • Cookie Policy
© Copyright Alachisoft 2002 - . All rights reserved. NCache is a registered trademark of Diyatech Corp.
  • Facebook
  • Twitter
  • Youtube
  • LinkedIn
  • RSS