global::memcached

This plugin exposes methods to open a pool of connexions to a memached server using Rhai.

fn connect

fn connect(parameters: Map) -> Cache
Open a pool of connections to a Memcached server.
  • parameters - a map of the following parameters:
    • url - a string url to connect to the server.
    • timeout - time allowed between each interaction with the server. (default: 30s)
    • connections - Number of connections to open to the server. (default: 4)

A service used to access the memcached server pointed by the url parameter.

  • The service failed to connect to the server.
// Import the plugin stored in the `plugins` directory.
import "plugins/libvsmtp_plugin_memcached" as cache;

export const cache = cache::connect(#{
    // Connect to a server on the port 11211 with a timeout.
    url: "memcache://localhost:11211",
    timeout: "10s",
    connections: 1,
});

fn flush

fn flush(cache: Cache) -> ()
Flush all cache on the server immediately

Build a service in services/cache.vsl;

// Import the plugin stored in the `plugins` directory.
import "plugins/libvsmtp_plugin_memcached" as cache;

export const srv = cache::connect(#{
    url: "memcache://localhost:11211",
    timeout: "10s",
    connections: 1,
});

Flush all cache during filtering.

import "services/cache" as srv;

#{
    connect: [
        action "flush the cache" || {
            srv::cache.flush();
        }
    ],
}

fn get

fn get(cache: Cache, key: String) -> ?
Get something from the server.
  • key - The key you want to get the value from

A rhai::Dynamic with the value inside

Build a service in services/cache.vsl;

// Import the plugin stored in the `plugins` directory.
import "plugins/libvsmtp_plugin_memcached" as cache;

export const srv = cache::connect(#{
    url: "memcache://localhost:11211",
    timeout: "10s",
    connections: 1,
});

Get the value wanted during filtering.

import "services/cache" as srv;

#{
    connect: [
        action "get value from my memcached server" || {
            // For the sake of this example, we assume that there is a "client_ip" as a key and "0.0.0.0" as its value.
            const client_ip = srv::cache.get("client_ip");
            log("info", `ip of my client is: ${client_ip}`);
        }
    ],
}

fn get_with_cas

fn get_with_cas(cache: Cache, key: String) -> ?
Get a value from the server with its cas_id and its expiration seconds.
  • key - The key you want to get the value from

A rhai::Dynamic with the values inside. Keys are “value”, “expiration”, “cas_id”. If the key doesn’t exist, returns 0

Build a service in services/cache.vsl;

// Import the plugin stored in the `plugins` directory.
import "plugins/libvsmtp_plugin_memcached" as cache;

export const srv = cache::connect(#{
    url: "memcache://localhost:11211",
    timeout: "10s",
    connections: 1,
});

Get the value wanted during filtering.

import "services/cache" as srv;

#{
    connect: [
        action "get value from my memcached server" || {
            // For the sake of this example, we assume that there is a "client_ip" as a key and "0.0.0.0" as its value.
            const cas_id = srv::cache.get_with_cas("client_ip").cas_id;
            log("info", `id is: ${cas_id}`);
        }
    ],
}

fn gets

fn gets(cache: Cache, keys: Array) -> ?
Gets multiple value from mutliple key from the server.
  • keys - The keys you want to get the values from

A rhai::Map<String, rhai::Dynamic> with the values inside

Build a service in services/cache.vsl;

// Import the plugin stored in the `plugins` directory.
import "plugins/libvsmtp_plugin_memcached" as cache;

export const srv = cache::connect(#{
    url: "memcache://localhost:11211",
    timeout: "10s",
    connections: 1,
});

Gets all the values wanted during filtering.

import "services/cache" as srv;

#{
    connect: [
        action "get value from my memcached server" || {
            // For the sake of this example, we assume that there is a server filled with multiple values
            const client_ips = srv::cache.gets(["client1_ip", "client2_ip", "client3_ip"]);
            log("info", `client 1: ${client_ips["client1_ip"]}`);
            log("info", `client 2: ${client_ips["client2_ip"]}`);
            log("info", `client 3: ${client_ips["client3_ip"]}`);
        }
    ],
}

fn gets_with_cas

fn gets_with_cas(cache: Cache, keys: Array) -> ?
Gets multiple value from mutliple key from the server with their cas_id, expiration seconds, key and value.
  • keys - The keys you want to get the values from

A rhai::Arrayrhai::Map with the values cas_id, expiration, key and value inside

Build a service in services/cache.vsl;

// Import the plugin stored in the `plugins` directory.
import "plugins/libvsmtp_plugin_memcached" as cache;

export const srv = cache::connect(#{
    url: "memcache://localhost:11211",
    timeout: "10s",
    connections: 1,
});

Gets all the values wanted during filtering.

import "services/cache" as srv;

#{
    connect: [
        action "get value from my memcached server" || {
            // For the sake of this example, we assume that there is a server filled with multiple values
            const entries = srv::memcached.gets_with_cas(["client1_ip", "client2_ip", "client3_ip"]);
            const cas_id = entries.find(|v| v.key == "client1_ip").cas_id;
            log("info", `id is: ${cas_id}`);
        }
    ],
}

fn set

fn set(cache: Cache, key: String, value: ?, duration: int) -> ()
Set a value with its associate key into the server with expiration seconds.
  • key - The key you want to allocate with the value
  • value - The value you want to store
  • duration - The duration time you want the value to remain in cache

Build a service in services/cache.vsl;

// Import the plugin stored in the `plugins` directory.
import "plugins/libvsmtp_plugin_memcached" as cache;

export const srv = cache::connect(#{
    url: "memcache://localhost:11211",
    timeout: "10s",
    connections: 1,
});

Set a value during filtering.

import "services/cache" as srv;

#{
    connect: [
        action "set value into my memcached server" || {
            srv::cache.set("client_ip", "0.0.0.0", 0);
            const client_ip = srv::cache.get("client_ip");
            log("info", `ip of my client is: ${client_ip}`);
        }
    ],
}

fn cas

fn cas(cache: Cache, key: String, value: ?, expiration: int, cas_id: int) -> bool
Compare and swap a key with the associate value into memcached server with expiration seconds.
  • key - The key you want to swap
  • value - The value you want to store
  • expiration - The duration time you want the value to remain in cache
  • cas_id - The id which is obtained from a previous call to gets

Build a service in services/cache.vsl;

// Import the plugin stored in the `plugins` directory.
import "plugins/libvsmtp_plugin_memcached" as cache;

export const srv = cache::connect(#{
    url: "memcache://localhost:11211",
    timeout: "10s",
    connections: 1,
});

Compare and swap a value during filtering

import "services/cache" as srv;

#{
    connect: [
        action "cas a key in the server" || {
            srv::cache.set("foo", "bar", 0);
            let result = srv::cache.get_with_cas("foo");
            srv::cache.cas("foo", "bar2", 0, result.cas_id);
        }
    ],
}

fn add

fn add(cache: Cache, key: String, value: ?, duration: int) -> ()
Add a key with associate value into memcached server with expiration seconds.
  • key - The key you want to allocate with the value
  • value - The value you want to store
  • duration - The duration time you want the value to remain in cache

Build a service in services/cache.vsl;

// Import the plugin stored in the `plugins` directory.
import "plugins/libvsmtp_plugin_memcached" as cache;

export const srv = cache::connect(#{
    url: "memcache://localhost:11211",
    timeout: "10s",
    connections: 1,
});

Add a value during filtering.

import "services/cache" as srv;

#{
    connect: [
        action "add value into my memcached server" || {
            // Will get an error if the key already exists
            srv::cache.add("client_ip", "0.0.0.0", 0);
            const client_ip = srv::cache.get("client_ip");
            log("info", `ip of my client is: ${client_ip}`);
        }
    ],
}

fn replace

fn replace(cache: Cache, key: String, value: ?, duration: int) -> ()
Replace a key with associate value into memcached server with expiration seconds.
  • key - The key you want to replace with the value
  • value - The value you want to store
  • duration - The duration time you want the value to remain in cache

Build a service in services/cache.vsl;

// Import the plugin stored in the `plugins` directory.
import "plugins/libvsmtp_plugin_memcached" as cache;

export const srv = cache::connect(#{
    url: "memcache://localhost:11211",
    timeout: "10s",
    connections: 1,
});

Replace a value during filtering.

import "services/cache" as srv;

#{
    connect: [
        action "replace value into my memcached server" || {
            srv::cache.set("client_ip", "0.0.0.0", 0);
            // Will get an error if the key doesn't exist
            srv::cache.replace("client_ip", "255.255.255.255", 0);
            const client_ip = srv::cache.get("client_ip");
            log("info", `ip of my client is: ${client_ip}`);
        }
    ],
}

fn append

fn append(cache: Cache, key: String, value: ?) -> ()
Append value to the key.
  • key - The key you want to append with the value
  • value - The value you want to append

Build a service in services/cache.vsl;

// Import the plugin stored in the `plugins` directory.
import "plugins/libvsmtp_plugin_memcached" as cache;

export const srv = cache::connect(#{
    url: "memcache://localhost:11211",
    timeout: "10s",
    connections: 1,
});

Append a value during filtering.

import "services/cache" as srv;

#{
    connect: [
        action "append value into my memcached server" || {
            srv::cache.set("client_ip", "0.0.", 0);
            // Will get an error if the key doesn't exist
            srv::cache.append("client_ip", "0.0");
            const client_ip = srv::cache.get("client_ip");
            log("info", `ip of my client is: ${client_ip}`);
        }
    ],
}

fn prepend

fn prepend(cache: Cache, key: String, value: ?) -> ()
Prepend value to the key.
  • key - The key you want to prepend with the value
  • value - The value you want to prepend

Build a service in services/cache.vsl;

// Import the plugin stored in the `plugins` directory.
import "plugins/libvsmtp_plugin_memcached" as cache;

export const srv = cache::connect(#{
    url: "memcache://localhost:11211",
    timeout: "10s",
    connections: 1,
});

Prepend a value during filtering.

import "services/cache" as srv;

#{
    connect: [
        action "prepend value into my memcached server" || {
            srv::cache.set("client_ip", ".0.0", 0);
            // Will get an error if the key doesn't exist
            srv::cache.prepend("client_ip", "0.0");
            const client_ip = srv::cache.get("client_ip");
            log("info", `ip of my client is: ${client_ip}`);
        }
    ],
}

fn delete

fn delete(cache: Cache, key: String) -> bool
Delete value of the specified key.
  • key - The key you want the value to be deleted

Build a service in services/cache.vsl;

// Import the plugin stored in the `plugins` directory.
import "plugins/libvsmtp_plugin_memcached" as cache;

export const srv = cache::connect(#{
    url: "memcache://localhost:11211",
    timeout: "10s",
    connections: 1,
});

Delete a value during filtering.

import "services/cache" as srv;

#{
    connect: [
        action "delete value into my memcached server" || {
            srv::cache.set("client_ip", "0.0.0.0", 0);
            srv::cache.delete("client_ip");
            // Will return nothing
            const client_ip = srv::cache.get("client_ip");
            log("info", `ip of my client is: ${client_ip}`);
        }
    ],
}

fn increment

fn increment(cache: Cache, key: String, value: int) -> ()
Increment value of the specified key.
  • key - The key you want the value to be incremented
  • value - Amount of the increment

Build a service in services/cache.vsl;

// Import the plugin stored in the `plugins` directory.
import "plugins/libvsmtp_plugin_memcached" as cache;

export const srv = cache::connect(#{
    url: "memcache://localhost:11211",
    timeout: "10s",
    connections: 1,
});

Increment a value during filtering.

import "services/cache" as srv;

#{
    connect: [
        action "increment value into my memcached server" || {
            srv::cache.set("nb_of_client", 1, 0);
            srv::cache.increment("nb_of_client", 21);
            const nb_of_client = srv::cache.get("nb_of_client");
            log("info", `nb of client is: ${nb_of_client}`);
        }
    ],
}

fn decrement

fn decrement(cache: Cache, key: String, value: int) -> ()
Decrement value of the specified key.
  • key - The key you want the value to be decremented
  • value - Amount of the Decrement

Build a service in services/cache.vsl;

// Import the plugin stored in the `plugins` directory.
import "plugins/libvsmtp_plugin_memcached" as cache;

export const srv = cache::connect(#{
    url: "memcache://localhost:11211",
    timeout: "10s",
    connections: 1,
});

Decrement a value during filtering.

import "services/cache" as srv;

#{
    connect: [
        action "decrement value into my memcached server" || {
            srv::cache.set("nb_of_client", 21, 0);
            srv::cache.decrement("nb_of_client", 1);
            const nb_of_client = srv::cache.get("nb_of_client");
            log("info", `nb of client is: ${nb_of_client}`);
        }
    ],
}

fn touch

fn touch(cache: Cache, key: String, duration: int) -> ()
Set a new expiration time for a exist key.
  • key - The key you want to change the expiration time
  • duration - Amount of expiration time

Build a service in services/cache.vsl;

// Import the plugin stored in the `plugins` directory.
import "plugins/libvsmtp_plugin_memcached" as cache;

export const srv = cache::connect(#{
    url: "memcache://localhost:11211",
    timeout: "10s",
    connections: 1,
});

Change an expiration time during filtering.

import "services/cache" as srv;

#{
    connect: [
        action "change expiration time of a value into my memcached server" || {
            srv::cache.set("nb_of_client", 21, 5000);
            srv::cache.touch("nb_of_client", 0);
            const nb_of_client = srv::cache.get("nb_of_client");
            log("info", `nb of client is: ${nb_of_client}`);
        }
    ],
}

fn stats

fn stats(cache: Cache) -> String
Only for debugging purposes, get all server's statistics in a formatted string

A formatted string

Build a service in services/cache.vsl;

// Import the plugin stored in the `plugins` directory.
import "plugins/libvsmtp_plugin_memcached" as cache;

export const srv = cache::connect(#{
    url: "memcache://localhost:11211",
    timeout: "10s",
    connections: 1,
});

Display the server statistics during filtering.

import "services/cache" as srv;

#{
    connect: [
        action "show statistics of my memcached server" || {
            const stats = srv::cache.stats();
            log("info", stats);
        }
    ],
}