定制 lmammino/guzzle-apcu-fs-cache 二次开发

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

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

lmammino/guzzle-apcu-fs-cache

Composer 安装命令:

composer require lmammino/guzzle-apcu-fs-cache

包简介

A zero config cache storage for guzzle-cache-middleware that tries to cache on Apcu and fallbacks to filesystem

README 文档

README

A zero config cache storage for guzzle-cache-middleware that tries to cache on Apcu and fallbacks to filesystem.

Tests Latest Stable Version Total Downloads Latest Unstable Version License composer.lock

Install

Requires PHP 5.5+.

Using composer:

composer require lmammino/guzzle-apcu-fs-cache

Usage

This is an example of how you can use this storage engine with Guzzle and GuzzleCacheMiddleware:

<?php

require_once __DIR__.'/vendor/autoload.php';

use GuzzleHttp\HandlerStack;
use GuzzleHttp\Handler\CurlHandler;
use GuzzleHttp\Client;
use Kevinrob\GuzzleCache\CacheMiddleware;
use Kevinrob\GuzzleCache\Strategy\PrivateCacheStrategy;
use LM\GuzzleCache\Storage\ApcuFsStorage;

// creates the storage (with default options)
$cacheStorage = new ApcuFsStorage();

// creates the cache middleware
$cacheMiddleware = new CacheMiddleware(
    new PrivateCacheStrategy($cacheStorage)
);

// creates a Guzzle client middleware stack
$stack = new HandlerStack();
$stack->setHandler(new CurlHandler());
$stack->push($cacheMiddleware, 'cache');
$client = new Client(['handler' => $stack]);

// make a request
$res = $client->request('GET', 'https://loige.co/');
// print response headers
$headers = $res->getHeaders();
foreach ($headers as  $k => $h) {
    foreach ($h as $v) {
        echo $k . ": " . $v . "\n";
    }
}
// print response body
echo "\n\n" . $res->getBody()->__toString() . "\n\n";

Use with Launch Darkly PHP SDK

If you want to use this cache storage with the Launch Darkly PHP SDK, here's how you can do it:

<?php

require_once __DIR__.'/vendor/autoload.php';

use LaunchDarkly\LDClient;
use LaunchDarkly\LDUser;
use LM\GuzzleCache\Storage\ApcuFsStorage;

$cacheStorage = new ApcuFsStorage();
$LDClient = new LDClient($ldSDKKey, ["cache" => $cacheStorage]);
$LDUser = new LDUser("test@example.com");

// use the client
var_dump($LDClient->variation('some-flag', $LDUser));

Configuration

The storage layer can be configured at construction time.

These are the following parameters accepted by the constructor:

  • (string) $dir: The directory where the cache should be saved if using the filesystem (Default: the temp directory).
  • (string) $namespace: A namespace for the cache storage, useful when using multiple instances and cache should not be mixed between them. It will create a subfolder on the filesystme and a prefix on APC. (Default: 'default').
  • (integer) $ttl The duration of a cache entry in seconds (Default: 60).
  • (callable) $onHit An optional function that gets called when there's a cache hit.
  • (callable) $onMiss An optional function that gets called when there's a cache miss.

If you want to inspect whether your current instance is using APC or the filesystem you can use the following public properties:

  • $storage->usingApcu (returns true or false)
  • $storage->usingFilesystem (returns true or false)

Rationale and Notes

This was originally created to cache requests made by the Launch Darkly client in a more controlled and configurable way.

Note: this middleware will always respect the TTL you provide and will ignore any HTTP cache header returned as response. This is by design. Use this middleware only if you want to enforce cache or for micro-caching scenarios (e.g. very expensive and frequent API calls).

Contributing

Everyone is very welcome to contribute to this project. You can contribute just by submitting bugs or suggesting improvements by opening an issue on GitHub.

License

Licensed under MIT License. © Luciano Mammino.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-04-01

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固