承接 dm1tra/cache-library 相关项目开发

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

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

dm1tra/cache-library

Composer 安装命令:

composer require dm1tra/cache-library

包简介

A simple caching library for PHP

README 文档

README

Installation

composer require dm1tra/cache-library

Usage

File Backend

require 'vendor/autoload.php';

try {
    $cache = new CacheLibrary\Cache('file', ['cache_dir' => '/path/to/cache']);
    $data = $cache->get('my_key');
    if (!$data) {
        $data = performExpensiveOperation();
        $cache->set('my_key', $data, 3600);
    }
    useData($data);
} catch (\Exception $e) {
    echo "Error: " . $e->getMessage();
}

Redis Backend

require 'vendor/autoload.php';

try {
    $cache = new CacheLibrary\Cache('redis', ['host' => '127.0.0.1', 'port' => 6379]);
    $data = $cache->get('my_key');
    if (!$data) {
        $data = performExpensiveOperation();
        $cache->set('my_key', $data, 3600);
    }
    useData($data);
} catch (\Exception $e) {
    echo "Error: " . $e->getMessage();
}

Memcached Backend

require 'vendor/autoload.php';

try {
    $cache = new CacheLibrary\Cache('memcached', ['host' => '127.0.0.1', 'port' => 11211]);
    $data = $cache->get('my_key');
    if (!$data) {
        $data = performExpensiveOperation();
        $cache->set('my_key', $data, 3600);
    }
    useData($data);
} catch (\Exception $e) {
    echo "Error: " . $e->getMessage();
}

Methods

set(string $key, $value, int $ttl = 3600): bool Set a value in the cache with a specified time-to-live (TTL).

Parameters:

  • $key: The key of the item to store.
  • $value: The value to store.
  • $ttl: The time-to-live in seconds.

Returns : True on success, false on failure. get(string $key) Get a value from the cache. Parameters :

  • $key: The key of the item to retrieve. Returns : The value of the item or null if not found. has(string $key): bool Check if an item exists in the cache.

Parameters :

  • $key: The key of the item to check. Returns : True if the item exists, false otherwise. delete(string $key): bool Delete an item from the cache.

Parameters :

  • $key: The key of the item to delete. Returns : True on success, false on failure. clear(): bool Clear all items from the cache.
  • Returns : True on success, false on failure.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-02-11