Try Playground
Show / Hide Table of Contents

Object Caching Using Cache API

ASP.NET Core applications can use NCache for object caching by simply using 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 to Use Cache API

  • .NET
  • Install the following NuGet packages in your application based on your NCache edition:
    • Enterprise: Alachisoft.NCache.SDK
    • Professional: Alachisoft.NCache.Professional.SDK
  • The cache must be running.
  • The application must be connected to cache before performing the operation.
  • For API details, refer to: CacheItem, Expiration, Insert.
  • Make sure that the data being added is serializable.
  • To ensure the operation is fail-safe, it is recommended to handle any potential exceptions within your application, as explained in Handling Failures.
  • To handle any unseen exceptions, refer to the Troubleshooting section.

Using NCache for Object Caching in ASP.NET Core

You can connect to an instance of the 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.AddMinutes(10);

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

See Also

Multi-Region ASP.NET Core Session Provider for NCache
Session Storage in ASP.NET Core
ASP.NET Core Response Caching

In This Article
  • Prerequisites to Use Cache API
  • Using NCache for Object Caching in ASP.NET Core
  • See Also

Contact Us

PHONE

+1 (214) 764-6933   (US)

+44 20 7993 8327   (UK)

 
EMAIL

sales@alachisoft.com

support@alachisoft.com

NCache
  • NCache Enterprise
  • NCache Professional
  • Edition Comparison
  • NCache Architecture
  • Benchmarks
Download
Pricing
Try Playground

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

  • EnglishChinese (Simplified)FrenchGermanItalianJapaneseKoreanPortugueseSpanish

  • Contact Us
  •  
  • Sitemap
  •  
  • Terms of Use
  •  
  • Privacy Policy
© Copyright Alachisoft 2002 - 2025. All rights reserved. NCache is a registered trademark of Diyatech Corp.
Back to top