定制 linio/cache 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

linio/cache

Composer 安装命令:

composer require linio/cache

包简介

Provides a multi-layered caching abstraction

README 文档

README

Latest Stable Version License Build Status Scrutinizer Code Quality

Linio Cache is yet another component of the Linio Framework. It aims to abstract caching by supporting multiple adapters.

Install

The recommended way to install Linio Cache is through composer.

{
    "require": {
        "linio/cache": "dev-master"
    }
}

Tests

To run the test suite, you need install the dependencies via composer, then run PHPUnit.

$ composer install
$ phpunit

Cache Not Found Keys

It is now possible (v.1.0.9) to cache not found keys in upper level adapters of the adapter stack. The configuration option cache_not_found_keys can be set at the adapter level.

Note that this option, obviously, does not apply to the last level of the cache hierarchy.

Usage

<?php

use \Linio\Component\Cache\CacheService;

$container['cache'] = new CacheService([
    'namespace' => 'mx',
    'layers' => [
        0 => [
            'adapter_name' => 'array',
            'adapter_options' => [
                'cache_not_found_keys' => true,
                'encoder' => 'json',
            ],
        ],
        1 => [
            'adapter_name' => 'apc',
            'adapter_options' => [
                'ttl' => 3600,
            ],
        ],
        2 => [
            'adapter_name' => 'redis',
            'adapter_options' => [
                'host' => 'localhost',
                'port' => 6379,
                'ttl' => 0,
                'encoder' => 'serial',
            ],
        ],
    ],
]);

$container->setLogger($container['logger']);

Note that must provide an adapter name and an array of options. Each adapter has different configuration options.

To start setting data:

<?php

$app['cache.service']->set('foo', 'bar');

Methods

get

<?php

    /**
     * @param string $key
     * @return string value
     */
    public function get($key);

    $adapter->get('foo');

getMulti

<?php

     /**
     * @param array $keys
     * @return string[]
     */
    public function getMulti(array $keys);

    $adapter->getMulti(['foo', 'nop']);

set

<?php

     /**
     * @param string $key
     * @param string $value
     * @param ?int $ttl Time To Live; store value in the cache for ttl seconds.
     * This ttl overwrites the configuration ttl of the adapter
     * @return bool
     */
    public function set(string $key, $value, ?int $ttl = null);

    $adapter->set('foo', 'bar');
    $adapter->set('foo', 'bar', 60); // store bar in the cache for 60 seconds

setMulti

<?php

     /**
     * @param array $keys
     * @return bool
     */
    public function setMulti(array $data);

    $adapter->setMulti(['foo' => 'bar', 'fooz' => 'baz']);

delete

<?php

     /**
     * @param string $key
     * @return bool
     */
    public function delete($key);

    $adapter->delete('foo');

deleteMulti

<?php

     /**
     * @param array $keys
     * @return bool
     */
    public function deleteMulti(array $keys);

    $adapter->deleteMulti(['foo', 'fooz']);

contains

<?php

     /**
     * @param string $key
     * @return bool
     */
    public function contains($key);

    $adapter->contains('foo');

flush

<?php

     /**
     * @return bool
     */
    public function flush();

    $adapter->flush();

Providers

array

This cache does not have any persistence between requests.

Not recommended to be used in production environments.

apc

Adapter options:

  • ttl optional default: 0 (unlimited)
  • cache_not_found_keys optional default: false

Requires APC extension or APCu extension.

wincache

Adapter options:

  • ttl optional default: 0 (unlimited)
  • cache_not_found_keys optional default: false

Requires WinCache extension.

memcached

Adapter options:

  • servers array of memcache servers. format: [[, , ], [, , ], ...]
  • options array of memcache options. format: [<option_name1> => , <option_name2> => , ...]
  • connection_persistent optional default: false
  • pool_size optional default: 1 (only for persistent connections)
  • ttl optional default: 0 (unlimited)
  • cache_not_found_keys optional default: false

Requires Memcached extension.

redis

Adapter options:

  • host optional default: 127.0.0.1
  • port optional default: 6379
  • database optional default: 0 (int)
  • password optional default: null (no password)
  • connection_persistent optional default: false
  • ttl optional default: 0 (unlimited)
  • cache_not_found_keys optional default: false

More information on the available parameters at the Predis documentation.

phpredis

Adapter options:

  • host optional default: 127.0.0.1
  • port optional default: 6379
  • database optional default: 0 (int)
  • password optional default: null (no password)
  • connection_persistent optional default: false
  • pool_size optional default: 1 (only for persistent connections)
  • timeout optional default: 0 (unlimited)
  • read_timeout optional default: 0 (unlimited)
  • retry_interval optional default: 0 (value in milliseconds)
  • ttl optional default: 0 (unlimited)
  • cache_not_found_keys optional default: false
  • serializer optional default: none
    • none don't serialize data
    • php use built-in serialize/unserialize
    • igbinary use igBinary serialize/unserialize (requires igbinary extension)

More information on the available parameters at the phpredis documentation.

Requires redis extension.

mysql

Using PDO.

Adapter options:

  • host
  • port
  • dbname
  • username
  • password
  • table_name
  • ensure_table_created optional default: false
  • cache_not_found_keys optional default: false

The ensure_table_created is used to ensure the cache table exists in the database. This option has a significant performance impact.

Not recommended to be used in production environments.

aerospike

Adapter options:

  • hosts
  • aerospike_namespace optional default: test
  • persistent optional default: true
  • options optional default: []
  • ttl optional default: 0 (unlimited)
  • cache_not_found_keys optional default: false

For the Aerospike adapter, the aerospike_namespace property will be used as the namespace in Aerospike, and the namespace configuration in the CacheService will be used as the set in Aerospike.

Requires Aerospike Extension.

linio/cache 适用场景与选型建议

linio/cache 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 29.59k 次下载、GitHub Stars 达 7, 最近一次更新时间为 2015 年 03 月 16 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「redis」 「memcache」 「cache」 「linio」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 linio/cache 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 linio/cache 我们能提供哪些服务?
定制开发 / 二次开发

基于 linio/cache 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

  • Stars: 7
  • Watchers: 49
  • Forks: 7
  • 开发语言: PHP

其他信息

  • 授权协议: BSD-3-Clause
  • 更新时间: 2015-03-16