承接 germania-kg/namespaced-cache 相关项目开发

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

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

germania-kg/namespaced-cache

最新稳定版本:1.4.0

Composer 安装命令:

composer require germania-kg/namespaced-cache

包简介

Factory classes for namespaced PSR-6 CacheItemPools

README 文档

README

Namespaced CacheItemPool Factory

Installation

$ composer require germania-kg/namespaced-cache

One of these libraries is required to be installed manually:

$ composer require symfony/cache
$ composer require tedivm/stash

Interfaces

Factory Interface

Classes implementing the PsrCacheItemPoolFactoryInterface are callable. Their invokation method accepts a namespace string.

<?php
Germania\NamespacedCache\PsrCacheItemPoolFactoryInterface;

interface PsrCacheItemPoolFactoryInterface
{
    public function __invoke( string $namespace) : \Psr\Cache\CacheItemPoolInterface;
}

DefaultLifeTimeAware

Define a default lifetime for cache items. It can be used on those PSR-6 libraries that support default life times on cache item pools.

<?php
use Germania\NamespacedCache\DefaultLifeTimeAware;

interface DefaultLifeTimeAware
{
    /**
     * Returns default cache item lifetime.
     *
     * @return int|null
     */
    public function getDefaultLifetime() : ?int;


    /**
     * Sets default cache item lifetime.
     *
     * @param int|null $lifetime Default cache item lifetime
     */
    public function setDefaultLifetime( ?int $lifetime);
}

Example:

<?php
use Germania\NamespacedCache\SymfonyFileCacheItemPoolFactory;
use Germania\NamespacedCache\DefaultLifeTimeAware;

$factory = new SymfonyFileCacheItemPoolFactory($directory);
if ($factory instanceOf DefaultLifeTimeAware::class) {
    $factory->setDefaultLifetime( 3600 );
}

Auto-discovering

Abstract class PsrCacheItemPoolFactory provides a static autodiscover method which will create SQLite or Filesystem cache factory, depending on if SQLite being available.

This is an experimental feature.

<?php
use Germania\NamespacedCache\PsrCacheItemPoolFactory;

$factory = PsrCacheItemPoolFactory::autodiscover($dsn_or_path);
$factory = PsrCacheItemPoolFactory::autodiscover($dsn_or_path, $default_lifetime);

Filesystem caches

Auto-discover Symfony Cache or Stash

Use this when migrating from one cache engine to another. It internally uses SymfonyFileCacheItemPoolFactory or StashFileCacheItemPoolFactory, whichever library is installed.

Callable class FileCacheItemPoolFactory implements PsrCacheItemPoolFactoryInterface.

<?php
use Germania\NamespacedCache\FileCacheItemPoolFactory;

# These are defaults
$directory = getcwd(); 
$default_lifetime = 0;

$factory = new FileCacheItemPoolFactory();
$factory = new FileCacheItemPoolFactory($directory, $default_lifetime);

// Psr\Cache\CacheItemPoolInterface
$cache = $factory("my_namespace");

echo get_class($cache);
// "Stash\Pool" or 
// "Symfony\Component\Cache\Adapter\FilesystemAdapter"

Symfony Cache Component

Callable class SymfonyFileCacheItemPoolFactory extends SymfonyCacheItemPoolFactory and implements PsrCacheItemPoolFactoryInterface and DefaultLifeTimeAware:

<?php
use Germania\NamespacedCache\SymfonyFileCacheItemPoolFactory;

# These are defaults
$directory = getcwd(); 
$default_lifetime = 0;

$factory = new SymfonyFileCacheItemPoolFactory();
$factory = new SymfonyFileCacheItemPoolFactory($directory, $default_lifetime);
$factory = (new SymfonyFileCacheItemPoolFactory($directory))
           ->setDefaultLifetime( 3600 );

// Psr\Cache\CacheItemPoolInterface
$cache = $factory("my_namespace");

Stash PHP Caching Library

Callable class StashFileCacheItemPoolFactory implements PsrCacheItemPoolFactoryInterface. Note that Stash caches do not provide setting default cache item lifetime.

<?php
use Germania\NamespacedCache\StashFileCacheItemPoolFactory;

# These are defaults
$directory = getcwd(); 

$factory = new StashFileCacheItemPoolFactory();
$factory = new StashFileCacheItemPoolFactory($directory);
  
// Psr\Cache\CacheItemPoolInterface
$cache = $factory("my_namespace");

Sqlite Caches

Auto-discover Symfony Cache or Stash

Use this when migrating from one cache engine to another. It internally uses SymfonySqliteCacheItemPoolFactory or StashSqliteCacheItemPoolFactory, whichever library is installed.

Callable class SqliteCacheItemPoolFactory implements PsrCacheItemPoolFactoryInterface.

Please note:

  • Symfony/Cache requires a DSN string
  • Stash/Cache requires a directory.
<?php
use Germania\NamespacedCache\SqliteCacheItemPoolFactory;

# These are defaults
$directory_or_dsn = getcwd(); 
$default_lifetime = 0;

$factory = new SqliteCacheItemPoolFactory($directory_or_dsn);
$factory = new SqliteCacheItemPoolFactory($directory_or_dsn, $default_lifetime);

// Psr\Cache\CacheItemPoolInterface
$cache = $factory("my_namespace");

echo get_class($cache);
// "Stash\Pool" or 
// "Symfony\Component\Cache\Adapter\PdoAdapter"

Symfony Cache Component

Callable class SymfonySqliteCacheItemPoolFactory extends SymfonyCacheItemPoolFactory and implements PsrCacheItemPoolFactoryInterface and DefaultLifeTimeAware.

<?php
use Germania\NamespacedCache\SymfonySqliteCacheItemPoolFactory;

# These are defaults
$dsn_or_directory = "sqlite::memory:"; 
$dsn_or_directory = "sqlite:/path/to/mydb.sq3";
$dsn_or_directory = "/tmp";
$default_lifetime = 0;

$factory = new SymfonySqliteCacheItemPoolFactory();
$factory = new SymfonySqliteCacheItemPoolFactory($dsn_or_directory, $default_lifetime);
$factory = (new SymfonySqliteCacheItemPoolFactory($dsn_or_directory))
           ->setDefaultLifetime( 3600 );

// Psr\Cache\CacheItemPoolInterface
$cache = $factory("my_namespace");

Stash PHP Caching Library

Callable class StashSqliteCacheItemPoolFactory implements PsrCacheItemPoolFactoryInterface.

Note that Stash caches do not provide setting default cache item lifetime.

<?php
use Germania\NamespacedCache\StashSqliteCacheItemPoolFactory;

# These are defaults
$directory = getcwd(); 

$factory = new StashSqliteCacheItemPoolFactory();
$factory = new StashSqliteCacheItemPoolFactory($directory);
  
// Psr\Cache\CacheItemPoolInterface
$cache = $factory("my_namespace");

Testing

$ composer phpunit

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-01-18