开发文档 v2.x

其它方法

更新时间:2023年10月7日 15:23 浏览:291
  • has - 指定键名的缓存是否存在
  • delete - 删除指定键名的缓存
  • increase - 自增缓存(针对数值缓存)
  • decrease - 自减缓存(针对数值缓存)

 

has - 指定键名的缓存是否存在

public function has(string $key): bool

参数:

  • string $key - 缓存键名

返回值:

  • bool - 是否存在

 

代码示例:

if (Be::getCache()->has('product:xxx')) {
  // 存在
}

 

delete - 删除指定键名的缓存

public function delete(string $key): bool

参数:

  • string $key - 键名

返回值:

  • bool - 是否删除成功

 

代码示例:

Be::getCache()->delete('product:xxx');

 

increase - 自增缓存(针对数值缓存)

public function increase(string $key, int $step = 1)

参数:

  • string $key - 键名
  • int $step - 自增量

返回值:

  • int | false - 自增操作后新的值,失败时返回 false

 

代码示例:

$cache = Be::getCache();

// 点击量加1
$hits = $cache->increase('product:xxx:hits');

// 点击量加10
$hits = $cache->increase('product:xxx:hits', 10);

 

decrease - 自减缓存(针对数值缓存)

public function decrease(string $key, int $step = 1)

参数:

  • string $key - 键名
  • int $step - 自减量

返回值:

  • int | false - 自减操作后新的值,失败时返回 false

 

代码示例:

$cache = Be::getCache();

// 商品 xxx 的库存
$cache->set('product:xxx:stock', 100);

$stock = $cache->decrease('product:xxx:stock', 2);
// 返回 98

 

导航