Caching

How to cache in memory.

Key takeaways

MemoryCache Stores values in the process memory.
NullCache Dummy cache implementation that simulates caching without any actual action.

Introduction

Pip.Services offers two components for caching a value. The first is MemoryCache, which stores values in memory. The second is the NullCache class, which is a dummy cache that can be used to simulate caching. The next two sections explain how to use both components.

MemoryCache

The MemoryCache component provides a way to create and manage a cache that stores values in the process memory.

Pre-requisites

In order to use the MemoryCache component, we need to import it. This can be done with the following command:

import { MemoryCache } from "pip-services3-components-nodex";
using PipServices3.Components.Cache;
import (
	cache "github.com/pip-services3-go/pip-services3-components-go/cache"
)
import 'package:pip_services3_components/pip_services3_components.dart';
from pip_services3_components.cache import MemoryCache
Not available

Create

We can create a memory cache by creating an instance of the MemoryCache class. The code below shows how to do it.

let myCache = new MemoryCache();
var myCache = new MemoryCache();
myCache := cache.NewMemoryCache()
var myCache = MemoryCache();
my_cache = MemoryCache()
Not available

Store

We can cache a value by using the store() method. This method has four input parameters. First, the correlation_id, which is a value that can be used to track execution through the call chain. Second, a key that can be used to uniquely identify the stored value. Third, the value to be stored. Lastly, the number of milliseconds that this value will be stored in memory. The code below shows how to use this method.

let myCachedValue = await myCache.store(null, "key1", "ABC", 180000);  // Returns "ABC"
var myCachedValue = await myCache.StoreAsync(null, "key1", "ABC", 180000);  // Returns "ABC"
myCachedValue, _ := myCache.Store("123", "key1", "ABC", 180000) // Returns "ABC"
var myCachedValue = await myCache.store(null, 'key1', 'ABC', 180000); // Returns "ABC"
my_cached_value = my_cache.store(None, "key1", "ABC", 180000)  # Returns "ABC"
Not available

Retrieve

To retrieve a cached value from memory, we can use the retrieve() method, which takes the correlation_id and the cached value’s key as input parameters. The code below shows how to use it.

let myRetrievedValue = await myCache.retrieve(null, "key1");  // Returns "ABC"
var myRetrievedValue = await myCache.RetrieveAsync<string>(null, "key1");  // Returns "ABC"
myRetrievedValue, _ := myCache.Retrieve("123", "key1") // Returns "ABC"
var myRetrievedValue = await myCache.retrieve(null, 'key1'); // Returns "ABC"
my_retrieved_value = my_cache.retrieve(None,"key1")   # Returns "ABC"
Not available

Remove

To remove a cached value from memory, we can use the remove() method. This method takes the correlation_id and the key of the value to be removed as input parameters. The example below shows how to use this method.

await myCache.RemoveAsync(null, "key1");
await myCache.RemoveAsync(null, "key1");
myCache.Remove("123", "key1")
await myCache.remove(null, 'key1');
my_cache.remove(None, "key1")
Not available

NullCache

The NullCache component is a dummy cache implementation. As such, it only simulates caching without any actual results.

Pre-requisites

In order to use the NullCache class, we need to import it. The following command shows how to do this.

import { NullCache } from "pip-services3-components-nodex";
using PipServices3.Components.Cache;
import (
	cache "github.com/pip-services3-go/pip-services3-components-go/cache"
)
import 'package:pip_services3_components/pip_services3_components.dart';
from pip_services3_components.cache import NullCache
Not available

Create

In order to create a dummy cache, we need to create an instance of the NullCache class. The code below shows how to do this.

let myCache = new NullCache();
var myCache = new NullCache();
myCache := cache.NewNullCache()
var myCache = NullCache();
my_cache = NullCache()
Not available

Store

To cache a value, we can use the store() method. This method takes four input parameters. First, the correlation_id, which is a value that can be used to track execution through the call chain. Second, a key that uniquely identifies the cached value. Third, the value to be cached. Lastly, the number of milliseconds that this value is going to be stored in memory. The following code provides an example of this method’s usage.

let myCachedValue = await myCache.store(null, "key1", "1234", 180000); // Returns "1234"
var myCachedValue = await myCache.StoreAsync(null, "key1", "1234", 180000); // Returns "1234"
myCachedValue, _ := myCache.Store("123", "key1", "1234", 180000) // Returns "1234"
var myCachedValue = await myCache.store(null, 'key1', '1234', 180000); // Returns "1234"
my_cached_value = my_cache.store(None, "key1", "1234", 180000) # Returns "1234"
Not available

Retrieve

To retrieve a cached value, we use the retrieve() method. This method takes two input parameters namely, the correlation_id and the key of the cached value to be retrieved. The following example shows how to use this method.

let myRetrievedValue = await myCache.retrieve(null, "key1"); // Returns null (Dummy caching only simulates retrieving))
var myRetrievedValue = await myCache.RetrieveAsync<string>(null, "key1"); // Returns null (Dummy caching only simulates retrieving)
myRetrievedValue, _ := myCache.Retrieve("123", "key1") // Returns None (Dummy caching only simulates retrieving)
var myRetrievedValue = await myCache.retrieve(null, 'key1'); // Returns null (Dummy caching only simulates retrieving))
my_retrieved_value = my_cache.retrieve(None, "key1") # Returns None (Dummy caching only simulates retrieving)
Not available

Remove

To remove a cached value, we can use the remove() method. This method asks for two input parameters, namely the correlation_id and the cached value’s key. The following example shows how to use it.

await myCache.remove(null, "key1");
await myCache.RemoveAsync(null, "key1");
myCache.Remove("123", "key1")
await myCache.remove(null, 'key1');
my_cache.remove(None, "key1")
Not available

Wrapping up

In this tutorial, we have seen how to cache a value for later use. First, we saw how to use the MemoryCache class, which provides a set of methods to store, retrieve and remove a value from memory. Then, we saw the NullCache class, which has the same methods as the MemoryCache class, but in practice doesn’t cache any value and only works as a dummy instrument.