Coder Social home page Coder Social logo

jerel / quick-cache Goto Github PK

View Code? Open in Web Editor NEW
13.0 4.0 1.0 156 KB

A quick and easy to use PSR-2 driver based caching library that will cache simple key/value pairs or call methods and cache their results.

Home Page: unruhdesigns.com

License: MIT License

Ruby 0.74% PHP 99.26%

quick-cache's Introduction

Quick Cache

A quick and easy to use PSR-2 driver based caching library that will cache simple key/value pairs or call methods and cache their results.

Build Status

Includes Drivers For

  • Redis (using the fantastic predis library)
  • APC (requires APC installation / extension) *
  • Filesystem

* APC driver doesn't support clearing class/method caches manually via $cache->clear().

Author

License

MIT License

Usage

First install it via composer by placing this in a composer.json file

{
	"require": {
	    "quick/cache": "v1.0.0"
	}
}

and running composer.phar install

Now you can use it in your code by calling the class:

$cache = new Quick\Cache;

$cache->set('name', 'Jerel Unruh', $ttl = 3600); /* will expire in 3600 seconds */

$name = $cache->get('name');

$cache->forget('name');

Let it call your methods for you and handle the results:

// you can pass any config items in an array
$cache = new Quick\Cache(array('driver' => 'redis'));

// pass any arguments as an array
$cache->method($this->UserModel, 'getUsersByGroup', array('admin', 'desc'), 3600);

$cache->method('Project\Model\UserModel', 'getUsersByGroup');

// clear all the data cached for this class
$cache->clear('Project\Model\UserModel');

// or just this class + method
$cache->clear('Project\Model\UserModel', 'getUsersByGroup');

Flush all cached items for this driver. Don't use this in production as it's expensive. Use it when taking an app from staging to live or etc.

$cache = new Quick\Cache;

$cache->flush();

Configuration

All configuration details can be set in the config files in vendor/quick/cache/config. There is also a config class that allows you to set details programmatically. The only thing that cannot be changed with the config class is the driver. It must be set in the global config file or when instantiating the class;

$cache = new Quick\Cache(array('driver' => 'file'));

$config = $cache->config_instance();

$config->set('cache_path', 'project/cache/');

At the same time we can run another instance with different configurations

$redis_cache = new Quick\Cache(array('driver' => 'redis'));

$redis_config = $redis_cache->config_instance();

$redis_config->set_many(array(
	'redis_connection' => array(
		'host'     => '127.0.0.1',
		'port'     => 6379,
		),
	'redis_prefix' => 'cache',
));

// if you set the connection details manually you must refresh the connection
$redis_cache->connect();

// Create an APC driver instance
$apc_cache = new Quick\Cache(array('driver' => 'apc'));

$apc_cache->set('name', 'Jerel Unruh', 3600);

Other methods

$cache_path = $config->get('cache_path');

$config_items = $config->get_all();

$config->load('custom_driver');

Testing

Quick Cache is unit tested using phpUnit. I use Guard to run my tests while I work, if you have it installed you can test like this:

cd ./vendor/quick/cache
guard

If not you can run them via phpUnit

cd ./vendor/quick/cache
phpunit

Do not run these tests on a production environment! It will FLUSH your database!

Errors

If Quick Cache encounters a serious error that it needs to tell you about (such as unwriteable directories) it will throw a QuickCacheException

quick-cache's People

Contributors

jerel avatar trea avatar

Stargazers

Sean Adams-Hiett avatar Gary Blankenship avatar Ryun Shofner avatar  avatar Doug Grubba avatar Alexis Galbourdin avatar Gustavo Rodriguez Baldera avatar gr0g avatar Aleks Seltenreich avatar Wilfred de Kok avatar Steve Williamson avatar Boris Strahija avatar Greg Aker avatar

Watchers

 avatar James Cloos avatar Gustavo Rodriguez Baldera avatar  avatar

Forkers

gbaldera

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.