adonis5-memcached-client

Memcached client for AdonisJS 5

Usage no npm install needed!

<script type="module">
  import adonis5MemcachedClient from 'https://cdn.skypack.dev/adonis5-memcached-client';
</script>

README

Adonis5-Memcached-Client

Memcached client for AdonisJS 5

typescript-image npm-image license-image

Based on Memcached and promisified for better developer experience.

Table of contents

Installation

npm i adonis5-memcached-client

Install provider:

node ace invoke adonis5-memcached-client
  • For other configuration, please update the config/memcached.ts.

Sample Usage

Import client from Adonis IoC and use it for getting access to the cache:

 import MemcachedClient from '@ioc:Adonis/Addons/Adonis5-MemcachedClient'

export default class CacheRepository {
    constructor() {
    }

    public async find<T>(key): Promise<T | undefined> {
        return MemcachedClient.get < T > (key)
    }
}

Client api

get

Get the value for the given key.

const value = await client.get('key');
  • key: String, the key

touch

Touches the given key.

await client.touch('key', 10);
  • key: String The key
  • lifetime: Number After how long should the key expire measured in seconds

gets

Get the value and the CAS id.

const { key, cas } = await client.gets('key', 10);
  • key: String, the key

getMulti

Retrieves a bunch of values from multiple keys.

const values = await client.getMulti(['key-1', 'key-2']);
  • keys: String[], all the keys that needs to be fetched

set

Stores a new value in Memcached.

const result = await client.set('foo', 'bar', 10);
  • key: String the name of the key
  • value: Mixed Either a buffer, JSON, number or string that you want to store.
  • lifetime: Number, how long the data needs to be replaced measured in seconds

replace

Replaces the value in memcached.

const result = await client.replace('foo', 'bar', 10);
  • key: String the name of the key
  • value: Mixed Either a buffer, JSON, number or string that you want to store.
  • lifetime: Number, how long the data needs to be replaced measured in seconds

add

Add the value, only if it's not in memcached already.

const result = await client.add('test-key', 'test-value', 60);
  • key: String the name of the key
  • value: Mixed Either a buffer, JSON, number or string that you want to store.
  • lifetime: Number, how long the data needs to be replaced measured in seconds

cas

Add the value, only if it matches the given CAS value.

const result = await client.cas('test', 'new-value', cas, 100);
  • key: String the name of the key
  • value: Mixed Either a buffer, JSON, number or string that you want to store.
  • lifetime: Number, how long the data needs to be replaced measured in seconds
  • cas: String the CAS value

append

Add the given value string to the value of an existing item.

await client.append('test', '-postfix')
  • key: String the name of the key
  • value: Mixed Either a buffer, JSON, number or string that you want to store.

prepend

Add the given value string to the value of an existing item.

const result = await client.prepend('test', 'prefix-')
  • key: String the name of the key
  • value: Mixed Either a buffer, JSON, number or string that you want to store.

incr

Increment a given key.

const result = await client.incr('test', 100)
  • key: String the name of the key
  • amount: Number The increment

decr

Decrement a given key.

const result = await client.decr('test', 100)
  • key: String the name of the key
  • amount: Number The decrement

del

Remove the key from memcached.

const result = await client.del('test')
  • key: String the name of the key

version

Retrieves the version number of your server.

const versionInfo = await client.version()

flush

Flushes the memcached server.

const results = await client.flush()

stats

Retrieves stats from your memcached server.

const statsInfo = await client.stats()

settings

Retrieves your settings for connected servers.

const settings = await client.settings()

slabs

Retrieves slabs information for connected servers.

const slabsInfo = await client.slabs()

items

Retrieves items information for connected servers.

const items = await client.items()

end

Closes all active memcached connections.

await client.end()