From 51183c348d28af8b62866e388aaf4a1cc9e3dd84 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Wed, 7 Mar 2012 21:59:54 -0600 Subject: [PATCH] Added "memory" (array based) cache driver for easier unit testing of application cache operations for developers. Signed-off-by: Taylor Otwell --- application/config/cache.php | 2 +- laravel/cache.php | 3 ++ laravel/cache/drivers/memory.php | 66 ++++++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 laravel/cache/drivers/memory.php diff --git a/application/config/cache.php b/application/config/cache.php index 73fd4c9c..a507ff66 100644 --- a/application/config/cache.php +++ b/application/config/cache.php @@ -11,7 +11,7 @@ | be used to increase the performance of your application by storing any | commonly accessed data in memory, a file, or some other storage. | - | A variety of awesome drivers are available for you to use with Laravel. + | A variety of great drivers are available for you to use with Laravel. | Some, like APC, are extremely fast. However, if that isn't an option | in your environment, try file or database caching. | diff --git a/laravel/cache.php b/laravel/cache.php index 261bbddc..723aa6eb 100644 --- a/laravel/cache.php +++ b/laravel/cache.php @@ -56,6 +56,9 @@ protected static function factory($driver) case 'memcached': return new Cache\Drivers\Memcached(Memcached::connection(), Config::get('cache.key')); + case 'memory': + return new Cache\Drivers\Memory; + case 'redis': return new Cache\Drivers\Redis(Redis::db()); diff --git a/laravel/cache/drivers/memory.php b/laravel/cache/drivers/memory.php new file mode 100644 index 00000000..60957c33 --- /dev/null +++ b/laravel/cache/drivers/memory.php @@ -0,0 +1,66 @@ +get($key))); + } + + /** + * Retrieve an item from the cache driver. + * + * @param string $key + * @return mixed + */ + protected function retrieve($key) + { + if (array_key_exists($key, $this->storage)) + { + return $this->storage[$key]; + } + } + + /** + * Write an item to the cache for a given number of minutes. + * + * + * // Put an item in the cache for 15 minutes + * Cache::put('name', 'Taylor', 15); + * + * + * @param string $key + * @param mixed $value + * @param int $minutes + * @return void + */ + public function put($key, $value, $minutes) + { + $this->storage[$key] = $value; + } + + /** + * Delete an item from the cache. + * + * @param string $key + * @return void + */ + public function forget($key) + { + unset($this->storage[$key]); + } + +} \ No newline at end of file