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.
- {
- "require":{
- "illuminate/cache":"4.1.*@dev",
- "illuminate/filesystem":"4.1.*@dev",
- "illuminate/redis":"4.1.*@dev"
- }
- }
Illuminate Cache – Filesystem Example
- use Illuminate\Cache\CacheManager;
- use Illuminate\Filesystem\Filesystem;
-
- 'files'=>new FileSystem(),
- 'cache.driver'=>'file',
- 'cache.path'=>'/yourcache/dir',
- 'cache.prefix'=>'somenamespace_'
- )
- );
-
- $cacheManager=new CacheManager($app);
- $cache=$cacheManager->driver();
-
- $em=$cache->get("cachekey");
- #echo "Added to Cache";
- #store some data in the cache for 10 minutes
- $cache->put("cachekey",$val, 10);
- $em=$val;
- }
Illuminate Cache – APC Example
- use Illuminate\Cache\CacheManager;
-
- 'cache.driver'=>'apc',
- 'cache.prefix'=>'somenamespace_'
- )
- );
-
- $cacheManager=new CacheManager($app);
- $cache=$cacheManager->driver();
-
- $em=$cache->get("cachekey");
- #echo "Added to Cache";
- #store some data in the cache for 10 minutes
- $cache->put("cachekey",$val, 10);
- $em=$val;
- }
Illuminate Cache – Redis Example (with authentication)
- use Illuminate\Cache\CacheManager;
- use Illuminate\Redis\Database as Redis;
-
- 'host'=>'yourserver.com',
- 'port'=>16250,
- 'scheme'=>'tcp',
- ),
- ));
-
- 'redis'=>$redis,
- 'cache.driver'=>'redis',
- 'cache.prefix'=>'somenamespace_'
- )
- );
-
- $cacheManager=new CacheManager($app);
- $cache=$cacheManager->driver();
- #authentication is optional depending on your situation
-
- $em=$cache->get("cachekey");
- #echo "Added to Cache";
- #store some data in the cache for 10 minutes
- $cache->put("cachekey",$val, 10);
- $em=$val;
- }
Would like to see a memcached example on here as well. Could you do that for me?