Cache Drivers

alvarofcgm edited this page Oct 18, 2016 · 1 revision

Index

Introduction

Cache Drivers are used to store and retrieve data, usually in form of key-value pairs.

Drivers inherit from CacheHandler abstract class. The difference between drivers is basically the support where they store data.

SharedMapCacheDriver: Stores data in an unordered_map in form of key-value pair or sets of key-value pairs.

RedisCacheDriver: Stores data using Redis data structure server in form of key-value pair or sets of key-value pairs.

These are the methods:

  • Write: Saves or updates a key-value pair.

  • Read : Reads a value with the given key.

  • Destroy: Destroys a key-value pair or a set of key-value pairs with the given name.

  • Rename: Renames a key if it exists.

  • make_iterator: Returns an iterator to iterate through keys that match an expression.

  • Match: Fills a vector of strings with the the keys that match an expression.

They are multi-thread safe.

Reference documentation

Cache Handler Reference Documentation

Examples

Instantiation

// a map cache driver:
#include "granada/cache/shared_map_cache_driver.h"
granada::cache::SharedMapCacheDriver map_cache_driver;
std::unique_ptr<granada::cache::CacheHandler> cache = std::make_unique<granada::cache::SharedMapCacheDriver>();

// a redis cache driver:
#include "granada/cache/redis_cache_driver.h"
granada::cache::RedisCacheDriver redis_cache_driver;
std::unique_ptr<granada::cache::CacheHandler> cache = std::make_unique<granada::cache::RedisCacheDriver>();

Write


// storing key-value pair:
cache_driver.Write("cart.item.type:johndoe:1","book");
cache_driver.Write("cart.item.price:johndoe:1","14");
cache_driver.Write("cart.item.quantity:johndoe:1","1");
cache_driver.Write("cart.item.type:johndoe:2","CD");
cache_driver.Write("cart.item.price:johndoe:2","8");
cache_driver.Write("cart.item.quantity:johndoe:2","2");

cache_driver.Write("cart:johndoe","[{\"type\":\"book\",\"price\":14,\"quantity\":1},{\"type\":\"CD\",\"price\":8,\"quantity\":2}]");


// storing sets of key value pairs:
cache_driver.Write("cart.item:johndoe:1","reference","1");
cache_driver.Write("cart.item:johndoe:1","type","book");
cache_driver.Write("cart.item:johndoe:1","price","14");
cache_driver.Write("cart.item:johndoe:1","quantity","1");
cache_driver.Write("cart.item:johndoe:2","reference","2");
cache_driver.Write("cart.item:johndoe:2","type","CD");
cache_driver.Write("cart.item:johndoe:2","price","8");
cache_driver.Write("cart.item:johndoe:2","quantity","2");

Read

const std::string& reference = "1";

// read value from a key-value pair
const std::string& type = cache_driver.Read("cart.item.type:johndoe:" + reference);

// read value from a set of key value pairs
const std::string& type = cache_driver.Read("cart.item:johndoe:"+reference,"type");
const std::string& price = cache_driver.Read("cart.item:johndoe:"+reference,"price");
const std::string& quantity = cache_driver.Read("cart.item:johndoe:"+reference,"quantity");

Destroy

const std::string& reference = "1";

// destroy a key-value pair
cache_driver.Destroy("cart.item.type:johndoe:" + reference);

// destroy key-value pair from a set of key-value pairs
cache_driver.Destroy("cart.item:johndoe:"+reference,"type");

// destroy a set of key value pairs:
cache_driver.Destroy("cart.item:johndoe:"+reference);

Rename

const std::string& old_reference = "1";
const std::string& new_reference = "1568";
cache_driver.Rename("cart.item:johndoe:"+old_reference,"cart.item:johndoe:"+new_reference);

make_iterator

// search all John Doe car items:
std::string hash = "cart.item:johndoe:*";
std::unique_ptr<granada::cache::CacheHandlerIterator> cache_iterator = cache_driver.make_iterator(hash);

while (cache_iterator.has_next()){
	const std::string& key = cache_iterator.next();
}

Match

// Fills a vector of strings with the the keys that match an expression.
std::vector keys;
cache_driver.Match("cart.item:johndoe:*",keys);
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.