ciatog/redis-cache

PHP Redis 缓存包装器

1.0.0 2015-01-20 03:55 UTC

This package is not auto-updated.

Last update: 2024-09-28 16:31:08 UTC


README

Build Status Coverage Status

PHP Redis Cache

Redis 的 PHP 缓存包装器

此库作为 Redis 的包装器,可以将其用作缓存。它具有以下功能

  • 设置唯一上下文的能力,以便您可以在不同情况下使用不同的缓存。例如,如果您需要使用相同的 Redis 服务器在不同站点之间使用缓存,则可以在创建缓存时传入站点 URL,以便每个站点都有自己的缓存上下文。
  • 在缓存中保存数据时自动序列化您的数据。
  • 从缓存检索数据时自动反序列化您的数据。
  • 允许传递匿名配置函数,如果项目不存在,则会调用该函数,并允许您生成所需的值。

使用方法

使用 composer require ciatog/redis-cache 安装最新版本

<?php

use Ciatog\RedisCache;

// Creates an instance of the cache using `MY_CONTEXT` as the unique context
// for all operations
$cache = new RedisCache("MY_CONTEXT");

// Returns true/false depending on whether an item with this key exists in the cache.
$cache->exists("TEST_KEY");

// Return data in cache for key `TEST_KEY`.
// If that key is not in the cache then execute the function passed as the
// second argument, set the item in the cache to the data set on $config->item
// and finally return the item data.
$cache->get(
    "TEST_KEY",
    function ($config) {
        $config->item = "Test Data";
    }
);

// Deletes the item in the cache with this key if it exists
$cache->delete("TEST_KEY");

// Deletes all items in the cache
$cache->deleteAll();

// Returns a list of all the keys in the cache
$cache->keys();

作者

Keith Webster - keith.webster@gmail.com - http://keith-webster.com

许可

PHP Redis Cache 使用 MIT 许可证授权 - 有关详细信息,请参阅 LICENSE.txt 文件