added documentation to main repository.
This commit is contained in:
38
documentation/cache/config.md
vendored
Normal file
38
documentation/cache/config.md
vendored
Normal file
@@ -0,0 +1,38 @@
|
||||
## Cache Configuration
|
||||
|
||||
- [Memcached](#memcached)
|
||||
- [Cache Keys](#keys)
|
||||
|
||||
Imagine your application displays the ten most popular songs as voted on by your users. Do you really need to look up these ten songs every time someone visits your site? What if you could store them for 10 minutes, or even an hour, allowing you to dramatically speed up your application? Caching makes it simple.
|
||||
|
||||
Laravel provides three wonderful cache drivers out of the box:
|
||||
|
||||
- File System
|
||||
- Memcached
|
||||
- APC
|
||||
|
||||
By default, Laravel is configured to use the **file** system cache driver. It's ready to go. The file system driver stores cached items as files in the **application/storage/cache** directory. If you're satisfied with this driver, no other configuration is required. You're ready to start using it.
|
||||
|
||||
> **Note:** Before using the file system cache driver, make sure your **application/storage/cache** directory is writeable.
|
||||
|
||||
<a name="memcached"></a>
|
||||
### Memcached
|
||||
|
||||
[Memcached](http://memcached.org) is an ultra-fast, open-source distributed memory object caching system used by sites such as Wikipedia and Facebook. Before using Laravel's Memcached driver, you will need to install and configure Memcached and the PHP Memcache extension on your server.
|
||||
|
||||
Once Memcached is installed on your server, configuring the Laravel driver is a breeze. First, set the **driver** in the **application/config/cache.php** file:
|
||||
|
||||
'driver' => 'memcached'
|
||||
|
||||
Next, add your Memcached servers to the **servers** array:
|
||||
|
||||
'servers' => array(
|
||||
array('host' => '127.0.0.1', 'port' => 11211, 'weight' => 100),
|
||||
)
|
||||
|
||||
<a name="keys"></a>
|
||||
### Cache Keys
|
||||
|
||||
To avoid naming collisions with other applications using APC or a Memcached server, Laravel prepends a **key** to each item stored in the cache using these drivers. Feel free to change this value:
|
||||
|
||||
'key' => 'laravel'
|
||||
53
documentation/cache/usage.md
vendored
Normal file
53
documentation/cache/usage.md
vendored
Normal file
@@ -0,0 +1,53 @@
|
||||
## Cache Usage
|
||||
|
||||
- [Storing Items](#put)
|
||||
- [Retrieving Items](#get)
|
||||
- [Removing Items](#forget)
|
||||
|
||||
<a name="put"></a>
|
||||
### Storing Items
|
||||
|
||||
Storing items in the cache is simple. Simply call the **put** method on the Cache class:
|
||||
|
||||
Cache::put('name', 'Taylor', 10);
|
||||
|
||||
The first parameter is the **key** to the cache item. You will use this key to retrieve the item from the cache. The second parameter is the **value** of the item. The third parameter is the number of **minutes** you want the item to be cached.
|
||||
|
||||
> **Note:** It is not necessary to serialize objects when storing them in the cache.
|
||||
|
||||
<a name="get"></a>
|
||||
### Retrieving Items
|
||||
|
||||
Retrieving items from the cache is even more simple than storing them. It is done using the **get** method. Just mention the key of the item you wish to retrieve:
|
||||
|
||||
$name = Cache::get('name');
|
||||
|
||||
By default, NULL will be returned if the cached item has expired or does not exist. However, you may pass a different default value as a second parameter to the method:
|
||||
|
||||
$name = Cache::get('name', 'Fred');
|
||||
|
||||
Now, "Fred" will be returned if the "name" cache item has expired or does not exist.
|
||||
|
||||
What if you need a value from your database if a cache item doesn't exist? The solution is simple. You can pass a closure into the **get** method as a default value. The closure will only be executed if the cached item doesn't exist:
|
||||
|
||||
$users = Cache::get('count', function() {return DB::table('users')->count();});
|
||||
|
||||
Let's take this example a step further. Imagine you want to retrieve the number of registered users for your application; however, if the value is not cached, you want to store the default value in the cache. It's a breeze using the **remember** method:
|
||||
|
||||
$users = Cache::remember('count', function() {return DB::table('users')->count();}, 5);
|
||||
|
||||
Let's talk through that example. If the **count** item exists in the cache, it will be returned. If it doesn't exist, the result of the closure will be stored in the cache for five minutes **and** be returned by the method. Slick, huh?
|
||||
|
||||
Laravel even gives you a simple way to determine if a cached item exists using the **has** method:
|
||||
|
||||
if (Cache::has('name'))
|
||||
{
|
||||
$name = Cache::get('name');
|
||||
}
|
||||
|
||||
<a name="forget"></a>
|
||||
### Removing Items
|
||||
|
||||
Need to get rid of a cached item? No problem. Just mention the name of the item to the **forget** method:
|
||||
|
||||
Cache::forget('name');
|
||||
Reference in New Issue
Block a user