Using Laravel 4’s Illuminate Cache as a Standalone – FileSystem, APC and Redis (with authentication)

Using Laravel 4’s Illuminate Cache as a Standalone – FileSystem, APC and Redis (with authentication)
5 (100%) 1 vote

This is more of a code snippet post more than anything, but I just wanted to give a more “thorough” example of how to use Illuminate Cache from Laravel 4 as a standalone. There are a few random snippets out there, but I just want to post some sample code to save some people a few minutes to get up and running right away.

I’ve done a fair mount of research on other caching abstraction libraries out there, and I keep coming back to Laravel’s implementation because of its simplicity and its support for a wide-variety of back-ends (e.g. APC, Memcached, Redis, WinCache, XCache, Filesystem, etc.).

The following snippets will cover using the Illuminate Cache as a standalone for the Filesystem- , APC-, Redis-based (with a password/authentication) cache.

  1. {
  2.     "require": {
  3.         "illuminate/cache": "4.1.*@dev",
  4.         "illuminate/filesystem": "4.1.*@dev",
  5.         "illuminate/redis": "4.1.*@dev"
  6.     }
  7. }

 Illuminate Cache – Filesystem Example

  1. use Illuminate\Cache\CacheManager;
  2. use Illuminate\Filesystem\Filesystem;
  3.  
  4. $app = array(
  5.     'files' => new FileSystem(),
  6.     'config' => array(
  7.         'cache.driver' => 'file',
  8.         'cache.path' => '/yourcache/dir',
  9.         'cache.prefix' => 'somenamespace_'
  10.     )
  11. );
  12.  
  13. $cacheManager = new CacheManager($app);
  14. $cache = $cacheManager->driver();
  15.  
  16. $em = $cache->get("cachekey");
  17. if(is_null($em)){
  18.     #echo "Added to Cache";
  19.     $val = array("data" =>1);
  20.     #store some data in the cache for 10 minutes
  21.     $cache->put("cachekey", $val, 10);
  22.     $em = $val;
  23. }
  24. var_dump($em);

Illuminate Cache – APC Example

  1. use Illuminate\Cache\CacheManager;
  2.  
  3. $app = array(
  4.     'config' => array(
  5.         'cache.driver' => 'apc',
  6.         'cache.prefix' => 'somenamespace_'
  7.     )
  8. );
  9.  
  10. $cacheManager = new CacheManager($app);
  11. $cache = $cacheManager->driver();
  12.  
  13. $em = $cache->get("cachekey");
  14. if(is_null($em)){
  15.     #echo "Added to Cache";
  16.     $val = array("data" =>1);
  17.     #store some data in the cache for 10 minutes
  18.     $cache->put("cachekey", $val, 10);
  19.     $em = $val;
  20. }
  21. var_dump($em);

 

Illuminate Cache – Redis Example (with authentication)

  1. use Illuminate\Cache\CacheManager;
  2. use Illuminate\Redis\Database as Redis;
  3.  
  4. $redis = new Redis(  array(
  5.     'default' => array(
  6.     'host'     => 'yourserver.com',
  7.     'port'     => 16250,
  8.     'scheme' => 'tcp',
  9. ),
  10. ));
  11.  
  12. $app = array(
  13.     'redis' => $redis,
  14.     'config' => array(
  15.         'cache.driver' => 'redis',
  16.         'cache.prefix' => 'somenamespace_'
  17.     )
  18. );
  19.  
  20. $cacheManager = new CacheManager($app);
  21. $cache = $cacheManager->driver();
  22. #authentication is optional depending on your situation
  23. $redis->command("AUTH", array("password" => 'yourpassword'));
  24.  
  25. $em = $cache->get("cachekey");
  26. if(is_null($em)){
  27.     #echo "Added to Cache";
  28.     $val = array("data" =>1);
  29.     #store some data in the cache for 10 minutes
  30.     $cache->put("cachekey", $val, 10);
  31.     $em = $val;
  32. }
  33. var_dump($em);
  • David

    Would like to see a memcached example on here as well. Could you do that for me?