Skip to content
/ cache Public

FSi Cache - Lightweight component that supports different cache storages with namespaces.

License

Notifications You must be signed in to change notification settings

fsi-open/cache

Repository files navigation

THIS PACKAGE IS DEPRECATED

Do not use this package, as it will not receive any updates and may be deleted in the future.

FSi Cache Component Documentation

Features:

  • supports array, APC and file cache storages. New storages will be added in the future.
  • supports namespaces inside of cache

By default all cache types use fsicache namespace, but you can change it using setNamespace method:

<?php

$cache->setNamespace('namespace-name');

You can also use option 'namespace' while creating new cache (it can be used in all types of cache).

<?php

$cache = new ArrayCache(array('namespace' => 'namespace-name'));

There is also a possibility to pass namespace as optional parameter into methods:

  • getItem($key, $namespace = null)
  • hasItem($key, $namespace = null)
  • addItem($key, $item, $lifetime = 0, $namespace = null)
  • setItem($key, $item, $lifetime = 0, $namespace = null)
  • removeItem($key, $namespace = null)

If $namespace is null the current namespace is taken from method getNamespace().

Setup and autoloading

We highly recommend to use autoloader generated by composer.phar.

Adding reflection into composer.json

{
    ...
    "require": {
        ...
        "fsi/cache": "0.9.*"
        ...
    },
    ...
}

Array Cache

Array cache should be used only in development environment to simulate normal cache behavior.

Example:

<?php

$cache = new ArrayCache();

APC Cache

APC cache require APC extension enabled in webserv. Informations about APC can be found at php.net

Example:

<?php

$cache = new ApcCache();

File Cache

File cache require cache directory path inside of variable $options['directory'] that is passed in constructor There is also an additional parameter $options['dirlvl'] that describe how deep cache should be nested. dirlvl parameter might be useful when you know that cache will hold a big amount of files. Higher dirlvl means less files in signle cache directory.

Example:

<?php

$cache = new FileCache(array('directory' => '/tmp', 'dirlvl' => 3));

Examples

Basic Usage

<?php

use FSi\Component\Cache\ApcCache;

// Create APC cache instance with default namespace.
$cache = new ApcCache();

// Check if there is a foo.
if ($cache->hasItem('foo')) {
    echo 'foo exists in cache!';
} else {
    // Store foo-value in cache under key foo for 3600 seconds.
    $cache->setItem('foo', 'foo-value', 3600);
}

Namespace Usage

<?php

use FSi\Component\Cache\ApcCache;

// Create APC cache instance with default namespace.
$cache = new ApcCache();

$cache->setItem('key1', 'test', 0, 'testnamespace1');
$cache->setItem('key2', 'test', 0, 'testnamespace2');

$cache->hasItem('key1', 'testnamespace1'); // Will return true.
$cache->hasItem('key2', 'testnamespace2'); // Will return true.

$cache->hasItem('key2', 'testnamespace1'); // Will return false.
$cache->hasItem('key1', 'testnamespace2'); // Will return false.

About

FSi Cache - Lightweight component that supports different cache storages with namespaces.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •  

Languages