Git Product home page Git Product logo

vpci's Introduction

VPCI (Vectorface PHP Caching Interface) Build Status


VPCI is a lightweight, straight forward cache interface that employs a strategy pattern to allow users to use multiple types of cache with a single unified interface. It has optional built in write through caching to handle information retrieval with a simple, single function call.

Using VPCI allows developers to manage information retrieval and caching without having to worry about imlpementation.


Contents

  1. Currently Supported Cache Types
  2. Configurability
  3. Use 2. Getting a Cache Object 2. Get 2. Set 2. Clean 2. Flush
  4. Prefix

Currently Supported Cache Types:

  • APC User Cache (for both PHP 5.4 and PHP 5.5)
  • Memcache
  • TempFileCache (A caching system that uses temporary files)

Top - Contents


Configurability

VPCI comes with a easy to set-up configuration system. Its default behavior is to select the "best" (ordered as in the Currently Supported section) caching system. The retrieval of the cache is done with a singleton, meaning that you will not loose your reference to your cache once it has been created. Instead, simply call CacheSingleton::GetCache() whenever the reference is needed.


Top - Contents


Use

Getting a Cache Object

VPCI has a singleton/factory that will chose the best available caching mechanism and return a cache object.

$cache = CacheSingleton::getCache();

VPCI is designed to run entirely on four commands: get(), set(), clean() and flush().

Get

The get function takes a single $key parameter. This is the key of the item in the cache. It returns the value stored with the given key.

$key = "cache_item";
$cachedData = $cache->get(key);

Write-Through

VPCI has an optional write-through cache feature built into the use() method. Above ignores the write-through functionality, using VPCI as a standard caching interface. The use() method can actually take three parameters and is defined as use($key, $callableArray = [], $ttl = -1). The parameters do the following:

  1. $key - The key used to retrieve the cached item
  2. $callableArray - An array with optional callable object("function") and argument array("args") that will be called if no item exists in the cache. The result will be returned
  3. $ttl - The amount of time to store the item in the cache, if the callable is called. If this is not set, the result will be returned but not stored in the cache.
// Will return data if it is in the cache
// Otherwise it will return false
$cachedData = $cache->get("key");

// Will return data if it is in the cache
// Otherwise will return result of callable
// Cache will not be updated
$cachedData = $cache->get("key", ["function" => "callable", "args" => []]);

// Will return data if it is in the cache
// Otherwise will return result of callable
// The result of the callable will be stored in the cache with key = "key" and ttl=3600
$cachedData = $cache->get("key", ["function" => "callable", "args" => []], 3600);

Set

The set function is used to store data in the cache. It takes 3 parameters:

  1. $key the key used to retrieve the cached item later
  2. $data the data to be stored in the cache
  3. $ttl the time to live of the cache item (the amount of time that the item should remain valid)
$key = "cache_item";
$data = "This will be stored in the cache"
$ttl = 3600 //This cache item will expire in one hour
$cache->set($key, $data, $ttl);

Clean

The clean function is used to clear any expired items out of the cache. Note that for many caching implementations this might not be necessary. Some cache implementations such as APC and Memcache clean themselves automatically.

$cache->clean();

Flush

The flush function deletes all items in the cache.

$cache->flush();

Top - Contents


Prefix

VPCI allows its users to define a cache prefix to use on every cache entry. This feature allows to create unique keys that will (hopefully) not conflict with any other process sharing the same cache.

The prefix can be setup in one of two ways. Either by modifying the program's configuration or by calling a setPrefix($prefix) method.

NOTE: Prefixes set with the setPrefix($prefix) method are volatile, meaning they are not retained after the program terminates.

Prefix Example

If you set the prefix variable to "vpci", for example:

{
	"cachePrefix": "vpci"
}

If you then set a cached item:

	$key = "key";
	$data = "example data";
	$ttl = 3600;

	$cache->set($key, $data, $ttl);

This item would be stored within the cache with a key of "vpci_key". However, to retrieve it with the prefix still set, you would simply call:

$data = $cache->get("key");

Top - Contents


vpci's People

Contributors

ckdarby avatar ethanaubuchon avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.