think-cache

think-cache is a component extracted from the thinkphp framework, and it has added connection pool functionality, automatically supporting both coroutine and non-coroutine environments.

Note
This manual is for webman v2. If you are using webman v1, please refer to the v1 version manual

Installation

composer require -W webman/think-cache

After installation, a restart is required (reload does not take effect).

Configuration File

The configuration file is config/think-cache.php.

Usage

  <?php
  namespace app\controller;

  use support\Request;
  use support\think\Cache;

  class UserController
  {
      public function db(Request $request)
      {
          $key = 'test_key';
          Cache::set($key, rand());
          return response(Cache::get($key));
      }
  }

Provided Interfaces

// Set cache
Cache::set('val','value',600);
// Check if cache is set
Cache::has('val');
// Get cache
Cache::get('val');
// Delete cache
Cache::delete('val');
// Clear cache
Cache::clear();
// Read and delete cache
Cache::pull('val');
// If not exists, write to cache
Cache::remember('val',10);

// For numeric cache data, you can use
// Increment cache by +1
Cache::inc('val');
// Increment cache by +5
Cache::inc('val',5);
// Decrement cache by -1
Cache::dec('val');
// Decrement cache by -5
Cache::dec('val',5);

// Use cache tags
Cache::tag('tag_name')->set('val','value',600);
// Delete cache data under a specific tag
Cache::tag('tag_name')->clear();
// Support specifying multiple tags
Cache::tag(['tag1','tag2'])->set('val2','value',600);
// Delete cache data under multiple tags
Cache::tag(['tag1','tag2'])->clear();

// Use multiple cache types
$redis = Cache::store('redis');

$redis->set('var','value',600);
$redis->get('var');