Skip to content
Snippets Groups Projects
Commit 071d178a authored by Sean Madsen's avatar Sean Madsen Committed by GitHub
Browse files

Merge pull request #257 from seanmadsen/cache-reference

Migrate "Cache reference" wiki page
parents ba120764 967595fe
Branches
No related tags found
No related merge requests found
# Cache Reference
## Using the cache
`Civi::cache()` is the simplest way to access the cache, automatically using the default cache type (described below). The `CRM_Utils_Cache_Interface` class lays out the methods for saving and retrieving cached items.
### Methods
* Set a cache value
```php
Civi::cache()->set('mykey', 'myvalue');
```
* Get a cached value
```php
Civi::cache()->get('mykey'); // returns the value, or NULL if not set
```
* Delete a cached value
```php
Civi::cache()->delete('mykey');
```
* Flush the entire cache
```php
Civi::cache()->flush();
```
### Example
```php
/**
* Finds the magic number, selecting one if necessary.
*
* @return int $magicNumber
* a magic number between 1 and 100
*/
function findMagicNumber() {
$magicNumber = Civi::cache()->get('magicNumber');
if (!$magicNumber) {
$magicNumber = rand(1,100);
Civi::cache()->set('magicNumber', $magicNumber);
}
return $magicNumber;
}
```
## Cache types
This is selected in `civicrm.settings.php`, where `CIVICRM_DB_CACHE_CLASS` is defined.
* "ArrayCache" - This is the default, using an in-memory array.
* "Memcache" - This is for the PHP Memcache extension.
* "Memcached" - This is for the PHP Memcached extension.
* "APCcache" - This is for the PHP APC extension.
* "NoCache" - This caches nothing
......@@ -63,7 +63,7 @@ pages:
- "API architecture": framework/api-architecture.md
- Asset Builder: framework/asset-builder.md
- Bootstrap Process: framework/bootstrap.md
# Cache: /framework/cache.md
- Cache Reference: framework/cache.md
# Components: /framework/components.md
# Database: /framework/database.md
- Schema definition: framework/schema-definition.md
......
......@@ -142,3 +142,4 @@ API+Examples api/examples
REST+interface api/interfaces#rest-interface
AJAX+Interface api/interfaces#ajax-interface
Smarty+API+interface api/interfaces#smarty-api-interface
Cache+Reference framework/cache-reference
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment