move documentation markdown to system directory.
This commit is contained in:
79
laravel/documentation/cache/config.md
vendored
Normal file
79
laravel/documentation/cache/config.md
vendored
Normal file
@@ -0,0 +1,79 @@
|
||||
# Cache Configuration
|
||||
|
||||
## Contents
|
||||
|
||||
- [The Basics](#the-basics)
|
||||
- [Database](#database)
|
||||
- [Memcached](#memcached)
|
||||
- [Redis](#redis)
|
||||
- [Cache Keys](#keys)
|
||||
- [In-Memory Cache](#memory)
|
||||
|
||||
<a name="the-basics"></a>
|
||||
## The Basics
|
||||
|
||||
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? Laravel's caching makes it simple.
|
||||
|
||||
Laravel provides five cache drivers out of the box:
|
||||
|
||||
- File System
|
||||
- Database
|
||||
- Memcached
|
||||
- APC
|
||||
- Redis
|
||||
- Memory (Arrays)
|
||||
|
||||
By default, Laravel is configured to use the **file** system cache driver. It's ready to go out of the box with no configuration. The file system driver stores cached items as files in the **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 **storage/cache** directory is writeable.
|
||||
|
||||
<a name="database"></a>
|
||||
## Database
|
||||
|
||||
The database cache driver uses a given database table as a simple key-value store. To get started, first set the name of the database table in **application/config/cache.php**:
|
||||
|
||||
'database' => array('table' => 'laravel_cache'),
|
||||
|
||||
Next, create the table on your database. The table should have three columns:
|
||||
|
||||
- key (varchar)
|
||||
- value (text)
|
||||
- expiration (integer)
|
||||
|
||||
That's it. Once your configuration and table is setup, you're ready to start caching!
|
||||
|
||||
<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 you must set the **driver** in the **application/config/cache.php** file:
|
||||
|
||||
'driver' => 'memcached'
|
||||
|
||||
Then, add your Memcached servers to the **servers** array:
|
||||
|
||||
'servers' => array(
|
||||
array('host' => '127.0.0.1', 'port' => 11211, 'weight' => 100),
|
||||
)
|
||||
|
||||
<a name="redis"></a>
|
||||
## Redis
|
||||
|
||||
[Redis](http://redis.io) is an open source, advanced key-value store. It is often referred to as a data structure server since keys can contain [strings](http://redis.io/topics/data-types#strings), [hashes](http://redis.io/topics/data-types#hashes), [lists](http://redis.io/topics/data-types#lists), [sets](http://redis.io/topics/data-types#sets), and [sorted sets](http://redis.io/topics/data-types#sorted-sets).
|
||||
|
||||
Before using the Redis cache driver, you must [configure your Redis servers](/docs/database/redis#config). Now you can just set the **driver** in the **application/config/cache.php** file:
|
||||
|
||||
'driver' => 'redis'
|
||||
|
||||
<a name="keys"></a>
|
||||
### Cache Keys
|
||||
|
||||
To avoid naming collisions with other applications using APC, Redis, 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'
|
||||
|
||||
<a name="memory"></a>
|
||||
### In-Memory Cache
|
||||
|
||||
The "memory" cache driver does not actually cache anything to disk. It simply maintains an internal array of the cache data for the current request. This makes it perfect for unit testing your application in isolation from any storage mechanism. It should never be used as a "real" cache driver.
|
||||
59
laravel/documentation/cache/usage.md
vendored
Normal file
59
laravel/documentation/cache/usage.md
vendored
Normal file
@@ -0,0 +1,59 @@
|
||||
# Cache Usage
|
||||
|
||||
## Contents
|
||||
|
||||
- [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.
|
||||
|
||||
You may also cache something "forever" if you do not want the cache to expire:
|
||||
|
||||
Cache::forever('name', 'Taylor');
|
||||
|
||||
> **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 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