此包已被放弃,不再维护。未建议替代包。

UserFrosting 缓存模块

4.5.0 2021-04-21 00:57 UTC

This package is auto-updated.

Last update: 2024-05-06 05:58:24 UTC


README

Latest Version Software License Join the chat at https://chat.userfrosting.com/channel/support Donate

分支 构建 覆盖率 风格
master
develop  

Louis Charette, 2017

为独立项目简化 Laravel 缓存系统的包装函数。有关所有缓存相关功能的详细信息,请参阅Laravel 文档。此包装支持 Laravel ArrayStoreFileStoreMemcachedStoreRedisStore

还包括一个 namespace 参数,用于处理同一服务器或项目上的多个缓存实例。

用法

对于任何存储驱动程序,您首先需要创建一个新的 *Store,传递每个存储的配置值。一旦创建了新的存储实例,您需要使用 instance 函数来获取 缓存实例

要使用以下存储之一,您首先需要将其添加到您的类或 PHP 文件中的 use 列表中。

如果您需要在同一服务器上使用多个缓存实例(特别是对于 Memcached 和 Redis 驱动程序)或同一项目上(例如基于用户的缓存),您可以在 *Store 构造函数中使用 namespace 参数。命名空间始终应该是 字符串

由于 Laravel 缓存 需要一个 Illuminate\Container\Container 实例,因此此包中的每个 *Store 都会自动创建一个。如果您想重用现有的 Container,您可以将它传递给 *Store 构造函数。

虽然 namespace 参数对于任何 *Store 都是必需的,但所有其他参数都是可选的,除非另有说明。

ArrayStore

ArrayStore 是一个虚拟存储,实际上并不保存任何内容。如果您想全局禁用缓存,可以使用它。

ArrayStore 接受 namespaceContainer 参数:new ArrayStore(string $namespace, Illuminate\Container\Container $app = null);

示例

use UserFrosting\Cache\ArrayStore;

...

$cacheStore = new ArrayStore("global");
$cache = $cacheStore->instance();

$cache->get(...);

FileStore

FileStore 将文件保存到文件系统。

FileStore 接受 namespacepathContainer 参数:new FileStore(string $namespace, string $path = "./", Illuminate\Container\Container $app = null);

示例

use UserFrosting\Cache\FileStore;

...

$cacheStore = new FileStore("global", "./cache");
$cache = $cacheStore->instance();

$cache->get(...);

MemcachedStore

MemcachedStore 使用 Memcached 高效地处理缓存。

Memcached 不要与 Memcache 混淆。它们是两个不同的事物!

MemcachedStore 接受 namespaceconfigContainer 参数:new MemcachedStore(string $namespace, array $config = [], Illuminate\Container\Container $app = null);

Memcached 配置数组包含您的 Memcached 实例的设置。默认值如下

[
  'host' => '127.0.0.1',
  'port' => 11211,
  'weight' => 100
]

可以使用此参数覆盖自定义配置。

示例

use UserFrosting\Cache\MemcachedStore;

...

$cacheStore = new MemcachedStore("global"); //Uses default Memcached settings
$cache = $cacheStore->instance();

$cache->get(...);

使用自定义服务器和端口的示例

use UserFrosting\Cache\MemcachedStore;

...

$cacheStore = new MemcachedStore("global", [
  'host' => '123.456.789.0',
  'port' => '22122'
]);
$cache = $cacheStore->instance();

$cache->get(...);

RedisStore

RedisStore 使用 Redis 服务器 高效地处理缓存。

RedisStore 接受以下参数:namespaceconfigContainernew RedisStore(string $namespace, array $config = [], Illuminate\Container\Container $app = null);

Redis 配置数组包含您 Redis 服务器设置。默认值如下

[
  'host' => '127.0.0.1',
  'password' => null,
  'port' => 6379,
  'database' => 0
]

可以使用此参数覆盖自定义配置。

示例

use UserFrosting\Cache\RedisStore;

...

$cacheStore = new RedisStore("global"); //Uses default Redis settings
$cache = $cacheStore->instance();

$cache->get(...);

示例:使用自定义端口和密码

use UserFrosting\Cache\RedisStore;

...

$cacheStore = new RedisStore("global", [
  'password' => 'MyAwesomePassword',
  'port' => '1234'
]);
$cache = $cacheStore->instance();

$cache->get(...);

样式指南

测试