承接 lufiipe/simplecache 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

lufiipe/simplecache

最新稳定版本:1.0.0

Composer 安装命令:

composer require lufiipe/simplecache

包简介

Simple file system cache library for PHP

README 文档

README

GitHub Release GitHub Actions Workflow Status GitHub License

SimpleCache

Simple cache library for PHP based on PSR-16. It uses a hybrid system: fast in-memory storage for temporary data and file storage for persistent data.

Install

composer require lufiipe/simplecache

Usage

use LuFiipe\SimpleCache\Cache;

$cache = new Cache();
$cache->set('key', 'value');
$value = $cache->get('key');

Setup the cache

The Cache class accepts the following arguments:

  • string $cacheDir : Path to a writable folder for storing cache files. If not provided, default system 'tmp' folder will be used.
  • null|int|\DateInterval $defaultTtl : Default Time to Live in secondes. By defaults 3600 secondes.
use LuFiipe\SimpleCache\Cache;

$cache = new Cache(__DIR__ . '/cache', 1800);

// Equivalent to:
$ttl = \DateInterval::createFromDateString('30 minutes');
$cache = new Cache(__DIR__ . '/cache', $ttl);

Retrieving items from the cache

Cache::get()

The get() method retrieves items from the cache. If the item is not found, it returns null. Alternatively, you can provide a second argument to specify a default value to return when the item is missing.

use LuFiipe\SimpleCache\Cache;

$cache = new Cache();

$value = $cache->get('key');

$value = $cache->get('key', 'default');

Cache::getMultiple()

The getMultiple() method retrieves multiple cache items by their keys.

use LuFiipe\SimpleCache\Cache;

$cache = new Cache();
$value = $cache->getMultiple(['key1', 'key2', 'key3']);

Like the get() method, you may pass a second argument to specifying the default value you wish to be returned if the item doesn't exist.

use LuFiipe\SimpleCache\Cache;

$cache = new Cache();
$cache->set('key1', 'value');

$value = $cache->getMultiple(['key1', 'badkey'], 'default');

The above example will output:

array(2) {
  [key1] => string(5) "value",
  [badkey] => string(7) "default",
}

Storing items in the cache

Cache::set()

The set() method store items in the cache. If the storage time is not passed to the set() method, the item will be stored using the cache instance's default TTL (by defaults 3600 secondes). Instead of passing the number of seconds as an integer, you may also pass a DateInterval instance representing the desired expiration time of the cached item.

use LuFiipe\SimpleCache\Cache;

$cache = new Cache();
$cache->set('key', 'value');

$cache->set('key', 'value', 28800);
$cache->set('key', 'value', new \DateInterval('PT8H'))

Cache::setMultiple()

setMultiple() is similar to set(), but instead of a single key/value item, it works on multiple items. The expiration time applies to all the items at once.

use LuFiipe\SimpleCache\Cache;

$cache = new Cache();
$cache->setMultiple([
    'key1' => 'value1',
    'key2' => 'value2',
]);

$cache->set('key', 'value', 28800);
$cache->set('key', 'value', new \DateInterval('PT8H'));

Removing items from the cache

Cache::delete()

You can delete items from the cache using the delete() method.

use LuFiipe\SimpleCache\Cache;

$cache = new Cache();

$cache->delete('key1');

Cache::deleteMultiple()

The deleteMultiple() method removes multiple cache items at once using a list of keys.

use LuFiipe\SimpleCache\Cache;

$cache = new Cache();

$cache->deleteMultiple(['key1', 'key2']);

Cache::clear()

You can clear the entire cache with the clear() method.

use LuFiipe\SimpleCache\Cache;

$cache = new Cache();

$cache->clear();

Checking if an item exists

The has() method checks whether an item exists in the cache. If the item is not found, it will return false.

use LuFiipe\SimpleCache\Cache;

$cache = new Cache();

if ($cache->has('key')) {
    // ...
}

Tests

By default, during testing, the cache uses the OS temporary files directory. To use a specific directory, copy the file phpunit.xml.dist to phpunit.xml and update the value of the CACHE_DIR variable.

Then, run:

php vendor/bin/phpunit

License

The MIT License (MIT). Please see License File for more information.

统计信息

  • 总下载量: 14
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-03-28

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固