Skip to content

Latest commit

 

History

History
73 lines (57 loc) · 2.34 KB

Cache.md

File metadata and controls

73 lines (57 loc) · 2.34 KB

1. Cache

Caching can be used in situations where data retreived from other places does not change often, or is slow to fetch.

Examples are Static lists in databases or entire web-pages that are static.

1.1. Instantiating a Cache

The cache in SPIN applications is always available through the cache() static class.

1.2. Configuring a cache

The cache is configured in the config-<environment>.json file under the caches key:

  "caches": {
    "apcu": {                                   // Name of Cache
      "adapter": "APCu",                        // Adaptername
      "class": "\\Spin\\Cache\\Adapters\\Apcu", // The class to load
      "options": {}                             // Optional options for class
    }
  }

1.3. Using multiple cache adaters

Using multiple caches is possible through the naming of the cache.

The config would look like this:

  "caches": {
    "apcu": {                                    // Name of Cache
      "adapter": "APCu",                         // Adaptername
      "class": "\\Spin\\Cache\\Adapters\\Apcu",  // The class to load
      "options": {}                              // Optional options for class
    },
    "redis": {                                   // Name of Cache
      "adapter": "Redis",                        // Adaptername
      "class": "\\Spin\\Cache\\Adapters\\Redis", // The class to load
      "options": {}                              // Optional options for class
    }
  }

If the code wants to use the "redis" cache then it would be accessed via cache('redis').

2. Examples

2.1 Setting a value

  # Set cache key 'aKey' = 'value' for 10 seconds
  $ok = cache()->set('aKey', 'value', 10);

2.1 Getting a value

  # Get cache key 'aKey' into $value
  $value = cache()->get('aKey');

3. Writing custom adapters

TBD