juanchosl/simplecache

PSR-16使用的多个缓存系统的小型读写集合

1.0.3 2024-06-19 22:50 UTC

This package is auto-updated.

Last update: 2024-09-23 21:58:17 UTC


README

描述

多个缓存系统的小型读写函数集合

安装

composer require juanchosl/simplecache

性能

从快到慢

  • 进程:仅对当前请求执行有效
  • 会话:仅对当前用户会话有效
  • Memcached:如果Memcached服务可用
  • Memcache:如果Memcached服务可用
  • Redis:如果Redis服务可用
  • 文件:最兼容的系统,文件存入文件系统,但较慢

如何使用它

直接使用可用的库之一

创建缓存实例

use JuanchoSL\SimpleCache\Repositories\ProcessCache;

$cache = new ProcessCache($_ENV['CACHE_ENVIRONMENT']);

写入缓存索引

如果没有设置时间,最大过期时间为30天

$result = $cache->set(string $cache_key, mixed $value, int $ttl = 0);

读取缓存索引

$cache_value = $cache->get(string $cache_key);

删除缓存索引

$result = $cache->delete(string $cache_key);

替换缓存索引

$result = $cache->replace(string $cache_key, mixed $new_value);

更改缓存索引的TTL(生存时间)

$result = $cache->touch(string $cache_key, int $new_ttl);

增加缓存索引的数值

$result = $cache->increment(string $cache_key, int $numeric_increment, int $stating_value_if_not_exists = 0, int $ttl_if_not_exists = $max_ttl);

减少缓存索引的数值

$result = $cache->decrement(string $cache_key, int $numeric_decrement, int $stating_value_if_not_exists = 0, int $ttl_if_not_exists = $max_ttl);

使用提供的适配器以兼容PSR-16 Simple-Cache

创建缓存实例

use JuanchoSL\SimpleCache\Repositories\ProcessCache;
use JuanchoSL\SimpleCache\Adapters\SimpleCacheAdapter;

$lib = new ProcessCache($_ENV['CACHE_ENVIRONMENT']);
$cache = new SimpleCacheAdapter($lib);

写入缓存索引

如果没有设置时间,最大过期时间为30天

$result = $cache->set(string $cache_key, mixed $value, int $ttl = 0);

检查缓存索引的可用性

$result = $cache->has(string $cache_key);

读取缓存索引

$cache_value = $cache->get(string $cache_key);

删除缓存索引

$result = $cache->delete(string $cache_key);