Skip to content

Commit

Permalink
Working on cache docs.
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed May 4, 2015
1 parent a0a7ee2 commit 763a729
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 39 deletions.
64 changes: 64 additions & 0 deletions cache.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
- [Accessing The Cache](#accessing-the-cache)
- [Cache Usage](#cache-usage)
- [Accessing Multiple Cache Stores](#accessing-multiple-cache-stores)
- [Adding Custom Cache Drivers](#adding-custom-cache-drivers)

<a name="configuration"></a>
## Configuration
Expand Down Expand Up @@ -178,3 +179,66 @@ Using the `Cache` facade, you may access various cache stores via the `store` me
$value = Cache::store('file')->get('foo');

Cache::store('redis')->put('bar', 'baz', 10);

<a name="adding-custom-cache-drivers"></a>
## Adding Custom Cache Drivers

To extend the Laravel cache with a custom driver, we will use the `extend` method on the `CacheManager`, which is used to bind a custom driver resolver to the manager, and is common across all manager classes. Typically, this is done within a [service provider](/docs/{{version}}/providers).

For example, to register a new cache driver named "mongo":

<?php namespace App\Providers;

use Cache;
use App\Extensions\MongoStore;
use Illuminate\Support\ServiceProvider;

class CacheServiceProvider extends ServiceProvider
{
/**
* Perform post-registration booting of services.
*
* @return void
*/
public function boot()
{
Cache::extend('mongo', function($app) {
return Cache::repository(new MongoStore);
});
}

/**
* Register bindings in the container.
*
* @return void
*/
public function register()
{
//
}
}

The first argument passed to the `extend` method is the name of the driver. This will correspond to your `driver` option in the `config/cache.php` configuration file. The second argument is a Closure that should return an `Illuminate\Cache\Repository` instance. The Closure will be passed an `$app` instance, which is an instance of the `Illuminate\Foundation\Application` [service container](/docs/{{version}}/container).

The call to `Cache::extend` could be done in the `boot` method of the default `App\Providers\AppServiceProvider` that ships with fresh Laravel applications, or you may create your own service provider to house the extension - just don't forget to register the provider in the `config/app.php` provider array.

To create our custom cache driver, we first need to implement the `Illuminate\Contracts\Cache\Store` [contract](/docs/{{version}}/contracts). So, our MongoDB cache implementation would look something like this:

class MongoStore implements Illuminate\Contracts\Cache\Store
{
public function get($key) {}
public function put($key, $value, $minutes) {}
public function increment($key, $value = 1) {}
public function decrement($key, $value = 1) {}
public function forever($key, $value) {}
public function forget($key) {}
public function flush() {}
}

We just need to implement each of these methods using a MongoDB connection. Once our implementation is complete, we can finish our custom driver registration:

Cache::extend('mongo', function($app) {
return Cache::repository(new MongoStore);
});

If you're wondering where to put your custom cache driver code, consider making it available on Packagist! Or, you could create an `Extensions` namespace within your `app` directory. However, keep in mind that Laravel does not have a rigid application structure and you are free to organize your application according to your preferences.
40 changes: 1 addition & 39 deletions extending.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# Extending The Framework

- [Managers & Factories](#managers-and-factories)
- [Cache](#cache)
- [Session](#session)
- [Authentication](#authentication)
- [Service Container Based Extension](#container-based-extension)
Expand All @@ -15,43 +14,6 @@ Each of these managers includes an `extend` method which may be used to easily i

> **Note:** Take a moment to explore the various `Manager` classes that ship with Laravel, such as the `CacheManager` and `SessionManager`. Reading through these classes will give you a more thorough understanding of how Laravel works under the hood. All manager classes extend the `Illuminate\Support\Manager` base class, which provides some helpful, common functionality for each manager.
<a name="cache"></a>
## Cache

To extend the Laravel cache facility, we will use the `extend` method on the `CacheManager`, which is used to bind a custom driver resolver to the manager, and is common across all manager classes. For example, to register a new cache driver named "mongo", we would do the following:

Cache::extend('mongo', function($app)
{
return Cache::repository(new MongoStore);
});

The first argument passed to the `extend` method is the name of the driver. This will correspond to your `driver` option in the `config/cache.php` configuration file. The second argument is a Closure that should return an `Illuminate\Cache\Repository` instance. The Closure will be passed an `$app` instance, which is an instance of `Illuminate\Foundation\Application` and a service container.

The call to `Cache::extend` could be done in the `boot` method of the default `App\Providers\AppServiceProvider` that ships with fresh Laravel applications, or you may create your own service provider to house the extension - just don't forget to register the provider in the `config/app.php` provider array.

To create our custom cache driver, we first need to implement the `Illuminate\Contracts\Cache\Store` contract. So, our MongoDB cache implementation would look something like this:

class MongoStore implements Illuminate\Contracts\Cache\Store {

public function get($key) {}
public function put($key, $value, $minutes) {}
public function increment($key, $value = 1) {}
public function decrement($key, $value = 1) {}
public function forever($key, $value) {}
public function forget($key) {}
public function flush() {}

}

We just need to implement each of these methods using a MongoDB connection. Once our implementation is complete, we can finish our custom driver registration:

Cache::extend('mongo', function($app)
{
return Cache::repository(new MongoStore);
});

If you're wondering where to put your custom cache driver code, consider making it available on Packagist! Or, you could create an `Extensions` namespace within your `app` directory. However, keep in mind that Laravel does not have a rigid application structure and you are free to organize your application according to your preferences.

<a name="session"></a>
## Session

Expand Down Expand Up @@ -172,7 +134,7 @@ For example, the `HashServiceProvider` binds a `hash` key into the service conta
public function boot()
{
parent::boot();

$this->app->bindShared('hash', function()
{
return new \Snappy\Hashing\ScryptHasher;
Expand Down

0 comments on commit 763a729

Please sign in to comment.