Home | Core

Introduction

Getting Started

Architecture

Services

Testing

Command Line Interface (CLI)

Configuration

Transactions

Security

How-To Guides

Upgrade Guides

Release Guides

V3 Testing

Cache

Core ships with a cache abstraction that makes it easy to switch from an in-memory cache to a redis-based one. The default driver that is shipped provides an in-memory cache but using your own cache driver is just as easy.

Prerequisites

Before we start, we need to establish what a few recurring variables and imports in this document refer to when they are used.

import { app, Container, Services } from "@arkecosystem/core-kernel";
  • The app import refers to the application instance which grants access to the container, configurations, system information and more.
  • The Container import refers to a namespace that contains all of the container specific entities like binding symbols and interfaces.
  • The Services import refers to a namespace that contains all of the core services. This generally will only be needed for type hints as Core is responsible for service creation and maintenance.

Cache Usage

Create an instance

const cacheStore: CacheStore = app.get>(Container.Identifiers.CacheFactory)()

Get all of the items in the cache

cacheStore.all();

Get the keys of the cache items

cacheStore.keys();

Get the values of the cache items

cacheStore.values();

Retrieve an item from the cache by key

cacheStore.get("key");

Retrieve multiple items from the cache by key

cacheStore.getMany(["key1", "key2"]);

Store an item in the cache for a given number of seconds

cacheStore.put("key", "value", 60);

Store multiple items in the cache for a given number of seconds

cacheStore.putMany(["value1", "value2"], 60);

Determine if an item exists in the cache

cacheStore.has("key");

Determine multiple items exist in the cache

cacheStore.hasMany(["key1", "key2"]);

Determine if an item doesn’t exist in the cache

cacheStore.missing("key");

Determine multiple items don’t exist in the cache

cacheStore.missingMany(["key1", "key2"]);

Store an item in the cache indefinitely

cacheStore.forever("key", "value");

Store multiple items in the cache indefinitely

cacheStore.foreverMany(["value1", "value2"]);

Remove an item from the cache

cacheStore.forget("key");

Remove multiple items from the cache

cacheStore.forgetMany(["key1", "key2"]);

Remove all items from the cache

cacheStore.flush();

Extending

As explained in a previous article, it is possible to extend Core services due to the fact that a Manager pattern is used. Lets go over a quick example of how you could implement your own cache store.

Implementing the Driver

Implementing a new driver is as simple as importing the cache store contract that needs to be satisfied and implement the methods specified in it.

import { Contracts } from "@arkecosystem/core-kernel";

export class MemoryCacheStore implements Contracts.Cache.CacheStore {
    private readonly store: Map = new Map();

    public async make(): Promise> {
        return this;
    }

    public async all(): Promise> {
        return Array.from(this.store.entries());
    }

    public async keys(): Promise {
        return Array.from(this.store.keys());
    }

    public async values(): Promise {
        return Array.from(this.store.values());
    }

    public async get(key: K): Promise {
        return this.store.get(key);
    }

    public async getMany(keys: K[]): Promise> {
        return keys.map((key: K) => this.store.get(key));
    }

    public async put(key: K, value: T, seconds?: number): Promise {
        this.store.set(key, value);

        return this.has(key);
    }

    public async putMany(values: Array<[K, T]>, seconds?: number): Promise {
        return Promise.all(values.map(async (value: [K, T]) => this.put(value[0], value[1])));
    }

    public async has(key: K): Promise {
        return this.store.has(key);
    }

    public async hasMany(keys: K[]): Promise {
        return Promise.all(keys.map(async (key: K) => this.has(key)));
    }

    public async missing(key: K): Promise {
        return !this.store.has(key);
    }

    public async missingMany(keys: K[]): Promise {
        return Promise.all([...keys].map(async (key: K) => this.missing(key)));
    }

    public async forever(key: K, value: T): Promise {
        // This in-memory store doesn't offer any persistence.
    }

    public async foreverMany(values: Array<[K, T]>): Promise {
        // This in-memory store doesn't offer any persistence.
    }

    public async forget(key: K): Promise {
        this.store.delete(key);

        return this.missing(key);
    }

    public async forgetMany(keys: K[]): Promise {
        return Promise.all(keys.map(async (key: K) => this.forget(key)));
    }

    public async flush(): Promise {
        this.store.clear();

        return this.store.size === 0;
    }

    public async getPrefix(): Promise {
        // This in-memory store doesn't offer any persistence.
    }
}

Implementing the service provider

Now that we have implemented our memory driver for the cache service, we can create a service provider to register it.

import { Container, Contracts, Providers, Services } from "@arkecosystem/core-kernel";

export class ServiceProvider extends Providers.ServiceProvider {
    public async register(): Promise {
        const cacheManager: Services.Cache.CacheManager = this.app.get(
            Container.Identifiers.CacheManager,
        );

        await cacheManager.extend("memory", MemoryCacheStore);
    }
}
  1. We retrieve an instance of the cache manager that is responsible for managing cache drivers.
  2. We call the extend method with an asynchronous function which is responsible for creating the cache store instance.

2020 © ARK.io | All rights reserved | An ARK.io Product