district5/cache-lib

Cache lib 是一个用于PHP的缓存库

2.0.0 2023-08-15 09:25 UTC

This package is auto-updated.

Last update: 2024-09-15 11:46:46 UTC


README

CacheLib 是一个灵活的PHP缓存库。

使用Composer

示例Composer文件内容

composer require district5/cache-lib

测试

如果扩展被加载(例如APC/Memcache/Memcached),则将对适配器进行测试。

$ composer install

$ ./vendor/bin/phpunit

快速开始

CacheLib公开以下用于缓存的函数

  • APC / APCU -
  • Memcache
  • Memcached
  • FileSystem

有关如何使用适配器的更多信息,请参阅tests/CacheLibTests/Adapters目录。

此外,还有一个AdapterNull,您可以在测试(或开发)目的中使用。

APC适配器

APC适配器将根据可用性使用apcapcu

<?php
$key = 'name';
$value = 'Joe Bloggs';

$adapter = new \District5\CacheLib\Adapters\AdapterApc(
    [
        'prefix' => '' // default
    ]
);
$adapter->get($key, $default = null); // returns false or mixed
$adapter->set($key, $value, $ttl = 86400); // returns bool
$adapter->has($key); // returns bool
$adapter->renew($key, $ttl); // returns bool
$adapter->remove($key); // returns bool
$adapter->setIfNotExists($key, $value, $ttl = 86400); // returns bool
$adapter->flush(); // returns bool

FileSystem适配器

APC适配器将根据可用性使用apcapcu

<?php
$key = 'name';
$value = 'Joe Bloggs';

$adapter = new \District5\CacheLib\Adapters\AdapterFileSystem(
    [
        'prefix' => '', // optional
        'path' => '/some/writable/directory'
    ]
);
$adapter->get($key, $default = null); // returns false or mixed
$adapter->set($key, $value, $ttl = 86400); // returns bool
$adapter->has($key); // returns bool
$adapter->renew($key, $ttl); // returns bool
$adapter->remove($key); // returns bool
$adapter->setIfNotExists($key, $value, $ttl = 86400); // returns bool
$adapter->flush(); // returns bool

Memcache适配器

Memcache适配器支持多个memcache服务器,类似于memcached适配器。

<?php
$key = 'name';
$value = 'Joe Bloggs';

$adapter = new \District5\CacheLib\Adapters\AdapterMemcache(
    [
        'prefix' => '', // optional
        'servers' => [
            [
                'host' => 'a-host-name',
                'port' => 11211,
                'timeout' => 60,
                'weight' => 1
            ],
            [
                'host' => 'another-host-name',
                'port' => 11211,
                'timeout' => 60,
                'weight' => 1
            ]
        ]
            
    ]
);
$adapter->get($key, $default = null); // returns false or mixed
$adapter->set($key, $value, $ttl = 86400); // returns bool
$adapter->has($key); // returns bool
$adapter->renew($key, $ttl); // returns bool
$adapter->remove($key); // returns bool
$adapter->setIfNotExists($key, $value, $ttl = 86400); // returns bool
$adapter->flush(); // returns bool

Memcached适配器

Memcached适配器支持多个服务器,类似于memcache适配器。

<?php
$key = 'name';
$value = 'Joe Bloggs';

$adapter = new \District5\CacheLib\Adapters\AdapterMemcached(
    [
        'prefix' => '', // optional
        'persistent_id' => '', // optional
        'servers' => [
            [
                'host' => 'a-host-name',
                'port' => 11211,
                'weight' => 1
            ],
            [
                'host' => 'another-host-name',
                'port' => 11211,
                'weight' => 1
            ]
        ]
            
    ]
);
$adapter->get($key, $default = null); // returns false or mixed
$adapter->set($key, $value, $ttl = 86400); // returns bool
$adapter->has($key); // returns bool
$adapter->renew($key, $ttl); // returns bool
$adapter->remove($key); // returns bool
$adapter->setIfNotExists($key, $value, $ttl = 86400); // returns bool
$adapter->flush(); // returns bool

Null适配器(用于测试)

null适配器对所有方法返回false。对于某些测试案例很有用。

<?php
$key = 'name';
$value = 'Joe Bloggs';

$adapter = new \District5\CacheLib\Adapters\AdapterNull([]);
$adapter->get($key, $default = null); // returns false
$adapter->set($key, $value, $ttl = 86400); // returns false
$adapter->has($key); // returns false
$adapter->renew($key, $ttl); // returns false
$adapter->remove($key); // returns false
$adapter->setIfNotExists($key, $value, $ttl = 86400); // returns false
$adapter->flush(); // returns false

在Mac上使APCU工作

$ pecl install apcu

将此内容添加到您的php.ini文件中(/usr/local/etc/php/7.4/php.ini

apc.enabled=on
apc.shm_size=64M
apc.enable_cli=on