-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcache.html
155 lines (104 loc) · 5.63 KB
/
cache.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<!DOCTYPE html>
<!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]> <html class="no-js lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]> <html class="no-js lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js"> <!--<![endif]-->
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>Cache</title>
<link rel="stylesheet" href="css/style.css">
<link rel="stylesheet" href="css/ubuntu-font.css">
<script src="vendor/modernizr-2.6.2.min.js"></script>
<script type="text/javascript" src="vendor/google-code-prettify/prettify.js"></script>
</head><body><section class="docs-content"><div class="container">
<article class="docs-body"><h1>Cache</h1>
<p><a name="configuration"></a></p>
<h2>Configuration</h2>
<p>Laravel provides a unified API for various caching systems. The cache configuration is located at <code>app/config/cache.php</code>. In this file you may specify which cache driver you would like used by default throughout your application. Laravel supports popular caching backends like <a href="http://memcached.org">Memcached</a> and <a href="http://redis.io">Redis</a> out of the box.</p>
<p>The cache configuration file also contains various other options, which are documented within the file, so make sure to read over these options. By default, Laravel is configured to use the <code>file</code> cache driver, which stores the serialized, cached objects in the filesystem. For larger applications, it is recommended that you use an in-memory cache such as Memcached or APC.</p>
<p><a name="cache-usage"></a></p>
<h2>Cache Usage</h2>
<p><strong>Storing An Item In The Cache</strong></p>
<pre class="prettyprint"><code>Cache::put('key', 'value', $minutes);
</code></pre>
<p><strong>Storing An Item In The Cache If It Doesn't Exist</strong></p>
<pre class="prettyprint"><code>Cache::add('key', 'value', $minutes);
</code></pre>
<p><strong>Checking For Existence In Cache</strong></p>
<pre class="prettyprint"><code>if (Cache::has('key'))
{
//
}
</code></pre>
<p><strong>Retrieving An Item From The Cache</strong></p>
<pre class="prettyprint"><code>$value = Cache::get('key');
</code></pre>
<p><strong>Retrieving An Item Or Returning A Default Value</strong></p>
<pre class="prettyprint"><code>$value = Cache::get('key', 'default');
$value = Cache::get('key', function() { return 'default'; });
</code></pre>
<p><strong>Storing An Item In The Cache Permanently</strong></p>
<pre class="prettyprint"><code>Cache::forever('key', 'value');
</code></pre>
<p>Sometimes you may wish to retrieve an item from the cache, but also store a default value if the requested item doesn't exist. You may do this using the <code>Cache::remember</code> method:</p>
<pre class="prettyprint"><code>$value = Cache::remember('users', $minutes, function()
{
return DB::table('users')->get();
});
</code></pre>
<p>You may also combine the <code>remember</code> and <code>forever</code> methods:</p>
<pre class="prettyprint"><code>$value = Cache::rememberForever('users', function()
{
return DB::table('users')->get();
});
</code></pre>
<p>Note that all items stored in the cache are serialized, so you are free to store any type of data.</p>
<p><strong>Removing An Item From The Cache</strong></p>
<pre class="prettyprint"><code>Cache::forget('key');
</code></pre>
<p><a name="increments-and-decrements"></a></p>
<h2>Increments & Decrements</h2>
<p>All drivers except <code>file</code> and <code>database</code> support the <code>increment</code> and <code>decrement</code> operations:</p>
<p><strong>Incrementing A Value</strong></p>
<pre class="prettyprint"><code>Cache::increment('key');
Cache::increment('key', $amount);
</code></pre>
<p><strong>Decrementing A Value</strong></p>
<pre class="prettyprint"><code>Cache::decrement('key');
Cache::decrement('key', $amount);
</code></pre>
<p><a name="cache-sections"></a></p>
<h2>Cache Sections</h2>
<blockquote>
<p><strong>Note:</strong> Cache sections are not supported when using the <code>file</code> or <code>database</code> cache drivers.</p>
</blockquote>
<p>Cache sections allow you to group related items in the cache, and then flush the entire section. To access a section, use the <code>section</code> method:</p>
<p><strong>Accessing A Cache Section</strong></p>
<pre class="prettyprint"><code>Cache::section('people')->put('John', $john);
Cache::section('people')->put('Anne', $anne);
</code></pre>
<p>You may also access cached items from the section, as well as use the other cache methods such as <code>increment</code> and <code>decrement</code>:</p>
<p><strong>Accessing Items In A Cache Section</strong></p>
<pre class="prettyprint"><code>$anne = Cache::section('people')->get('Anne');
</code></pre>
<p>Then you may flush all items in the section:</p>
<pre class="prettyprint"><code>Cache::section('people')->flush();
</code></pre>
<p><a name="database-cache"></a></p>
<h2>Database Cache</h2>
<p>When using the <code>database</code> cache driver, you will need to setup a table to contain the cache items. Below is an example <code>Schema</code> declaration for the table:</p>
<pre class="prettyprint"><code>Schema::create('cache', function($table)
{
$table->string('key')->unique();
$table->text('value');
$table->integer('expiration');
});
</code></pre>
</article><div class="clearfix"></div>
</div>
</section>
<script src="js/jquery.min.js"></script>
<script src="js/plugins.js"></script>
<script type="text/javascript" src="vendor/google-code-prettify/run_prettify.js"></script>
</body></html>