This file is indexed.

/usr/share/php/kohana3.2/modules/cache/guide/cache/usage.md is in libkohana3.2-mod-cache-php 3.2.0-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# Kohana Cache usage

[Kohana_Cache] provides a simple interface allowing getting, setting and deleting of cached values. Two interfaces included in _Kohana Cache_ additionally provide _tagging_ and _garbage collection_ where they are supported by the respective drivers.

## Getting a new cache instance

Creating a new _Kohana Cache_ instance is simple, however it must be done using the [Cache::instance] method, rather than the traditional `new` constructor.

     // Create a new instance of cache using the default group
     $cache = Cache::instance();

The default group will use whatever is set to [Cache::$default] and must have a corresponding [configuration](cache.config) definition for that group.

To create a cache instance using a group other than the _default_, simply provide the group name as an argument.

     // Create a new instance of the memcache group
     $memcache = Cache::instance('memcache');

If there is a cache instance already instantiated then you can get it directly from the class member.

 [!!] Beware that this can cause issues if you do not test for the instance before trying to access it.

     // Check for the existance of the cache driver
     if (isset(Cache::$instances['memcache']))
     {
          // Get the existing cache instance directly (faster)
          $memcache = Cache::$instances['memcache'];
     }
     else
     {
          // Get the cache driver instance (slower)
          $memcache = Cache::instance('memcache');
     }

## Setting and getting variables to and from cache

The cache library supports scalar and object values, utilising object serialization where required (or not supported by the caching engine). This means that the majority or objects can be cached without any modification.

 [!!] Serialisation does not work with resource handles, such as filesystem, curl or socket resources.

### Setting a value to cache

Setting a value to cache using the [Cache::set] method can be done in one of two ways; either using the Cache instance interface, which is good for atomic operations; or getting an instance and using that for multiple operations.

The first example demonstrates how to quickly load and set a value to the default cache instance.

     // Create a cachable object
     $object = new stdClass;

     // Set a property
     $object->foo = 'bar';

     // Cache the object using default group (quick interface) with default time (3600 seconds)
     Cache::instance()->set('foo', $object);

If multiple cache operations are required, it is best to assign an instance of Cache to a variable and use that as below.

     // Set the object using a defined group for a defined time period (30 seconds)
     $memcache = Cache::instance('memcache');
     $memcache->set('foo', $object, 30);

#### Setting a value with tags

Certain cache drivers support setting values with tags. To set a value to cache with tags using the following interface.

     // Get a cache instance that supports tags
     $memcache = Cache::instance('memcachetag');

     // Test for tagging interface
     if ($memcache instanceof Cache_Tagging)
     {
          // Set a value with some tags for 30 seconds
          $memcache->set('foo', $object, 30, array('snafu', 'stfu', 'fubar'));
     }
     // Otherwise set without tags
     else
     {
          // Set a value for 30 seconds
          $memcache->set('foo', $object, 30);
     }

It is possible to implement custom tagging solutions onto existing or new cache drivers by implementing the [Cache_Tagging] interface. Kohana_Cache only applies the interface to drivers that support tagging natively as standard.

### Getting a value from cache

Getting variables back from cache is achieved using the [Cache::get] method using a single key to identify the cache entry.

     // Retrieve a value from cache (quickly)
     $object = Cache::instance()->get('foo');

In cases where the requested key is not available or the entry has expired, a default value will be returned (__NULL__ by default). It is possible to define the default value as the key is requested.

     // If the cache key is available (with default value set to FALSE)
     if ($object = Cache::instance()->get('foo', FALSE))
     {
          // Do something
     }
     else
     {
          // Do something else
     }

#### Getting values from cache using tags

It is possible to retrieve values from cache grouped by tag, using the [Cache::find] method with drivers that support tagging.

 [!!] The __Memcachetag__ driver does not support the `Cache::find($tag)` interface and will throw an exception.

     // Get an instance of cache
     $cache = Cache::instance('memcachetag');

     // Wrap in a try/catch statement to gracefully handle memcachetag
     try
     {
          // Find values based on tag
          return $cache->find('snafu');
     }
     catch (Cache_Exception $e)
     {
          // Handle gracefully
          return FALSE;
     }

### Deleting values from cache

Deleting variables is very similar to the getting and setting methods already described. Deleting operations are split into three categories:

 - __Delete value by key__. Deletes a cached value by the associated key.
 - __Delete all values__. Deletes all caches values stored in the cache instance.
 - __Delete values by tag__. Deletes all values that have the supplied tag. This is only supported by Memcached-Tag and Sqlite.

#### Delete value by key

To delete a specific value by its associated key:

     // If the cache entry for 'foo' is deleted
     if (Cache::instance()->delete('foo'))
     {
          // Cache entry successfully deleted, do something
     }

By default a `TRUE` value will be returned. However a `FALSE` value will be returned in instances where the key did not exist in the cache.

#### Delete all values

To delete all values in a specific instance:

     // If all cache items where deleted successfully
     if (Cache::instance()->delete_all())
     {
           // Do something
     }

It is also possible to delete all cache items in every instance:

     // For each cache instance
     foreach (Cache::$instances as $group => $instance)
     {
          if ($instance->delete_all())
          {
               var_dump('instance : '.$group.' has been flushed!');
          }
     }

#### Delete values by tag

Some of the caching drivers support deleting by tag. This will remove all the cached values that are associated with a specific tag. Below is an example of how to robustly handle deletion by tag.

     // Get cache instance
     $cache = Cache::instance();

     // Check for tagging interface
     if ($cache instanceof Cache_Tagging)
     {
           // Delete all entries by the tag 'snafu'
           $cache->delete_tag('snafu');
     }

#### Garbage Collection

Garbage Collection (GC) is the cleaning of expired cache entries. For the most part, caching engines will take care of garbage collection internally. However a few of the file based systems do not handle this task and in these circumstances it would be prudent to garbage collect at a predetermined frequency. If no garbage collection is executed, the resource storing the cache entries will eventually fill and become unusable.

When not automated, garbage collection is the responsibility of the developer. It is prudent to have a GC probability value that dictates how likely the garbage collection routing will be run. An example of such a system is demonstrated below.

     // Get a cache instance
     $cache_file = Cache::instance('file');

     // Set a GC probability of 10%
     $gc = 10;

     // If the GC probability is a hit
     if (rand(0,99) <= $gc and $cache_file instanceof Cache_GarbageCollect)
     {
          // Garbage Collect
          $cache_file->garbage_collect();
     }

# Interfaces

Kohana Cache comes with two interfaces that are implemented where the drivers support them:

 - __[Cache_Tagging] for tagging support on cache entries__
    - [Cache_MemcacheTag]
    - [Cache_Sqlite]
 - __[Cache_GarbageCollect] for garbage collection with drivers without native support__
    - [Cache_File]
    - [Cache_Sqlite]

When using interface specific caching features, ensure that code checks for the required interface before using the methods supplied. The following example checks whether the garbage collection interface is available before calling the `garbage_collect` method.

    // Create a cache instance
    $cache = Cache::instance();

    // Test for Garbage Collection
    if ($cache instanceof Cache_GarbageCollect)
    {
         // Collect garbage
         $cache->garbage_collect();
    }