naucon / storage
存储抽象层。
Requires
- php: ^8.1
- doctrine/common: ^3.0.3
- naucon/registry: ~1.0
- naucon/utility: ~1.0
Requires (Dev)
- doctrine/orm: ^2.0
- phpunit/phpunit: ^9.0
- predis/predis: ^1.1
- psr/cache: ~1.0
- psr/simple-cache: ~1.0
- symfony/http-foundation: ^4.0 || ^5.0
Suggests
- doctrine/orm: storage adapter for doctrine2
- predis/predis: storage adapter for redis
- psr/cache: storage adapter to PSR-6 Cache
- psr/simple-cache: storage adapter to PSR-16 Simple Cache
- symfony/http-foundation: storage adapter for symfony session storage
README
关于
此包是键值存储(存储)的抽象层。目标是解耦具体的键值存储与业务逻辑。
功能
- 存储管理器作为存储或存储的基本 CRUD 操作的抽象层。
- 存储工厂和注册表来管理多个存储或存储。
- 链式存储以在多个存储或存储中保存实体。
- 支持复合标识符/键。
适配器
- 数组(内存中)
- 文件系统
- 原生会话
- 到 symfony 会话处理器的桥梁
- redis (predis) - 需要 redis 2.8 或更高版本
- doctrine ORM
- PSR-6 缓存
- PSR-16 简单缓存
- null 存储(不存储任何内容,无内存)
兼容性
- PHP 7.1 到 7.4
安装
通过 composer 安装最新版本
composer require naucon/storage
用法
入门
要使用存储,我们需要一个模型(普通的 PHP 对象)。此模型将被序列化,因此最好实现 Serializable
接口 - 即使不是强制性的。
class Product implements \Serializable { protected $id; protected $sku; protected $description; public function getId() { return $this->id; } public function setId($id) { $this->id = $id; } public function getSku() { return $this->sku; } public function setSku($sku) { $this->sku = $sku; } public function getDescription() { return $this->description; } public function setDescription($description) { $this->description = $description; } }
有了定义的模型,我们就能创建一个 StorageManager
。StorageManager
提供所有基本的 CRUD 操作 create()
、has()
、find()
、flush()
、remove()
。要工作,它需要一个存储提供者。提供者实现具体的存储或存储。在此示例中,我们使用 ArrayStorage
,但您可以选择任何其他支持的提供者或构建满足您需求的自定义存储。
use Naucon\Storage\Provider\ArrayStorage; use Naucon\Storage\StorageManager; $adapter = new ArrayStorage(Product::class); $manager = new StorageManager($adapter);
要将条目保存到存储中,请调用 flush($identifier, $model)
$model = new Product(); $model->setId(1); $model->setSku('U123'); $model->setDescription('Dragon fruit'); $manager->flush(1, $model);
要创建模型的新实例,您可以调用 StorageManager::create()
。需要实现 CreateAwareInterface
的存储。创建的实例将不会在存储中,直到您调用 flush()
。
$model = $manager->create(); $model->setId(1); $model->setSku('U123'); $model->setDescription('Dragon fruit'); $manager->flush(1, $model);
要从存储中检索条目,请调用 StorageManager::find($identifier)
。如果没有找到条目,您将返回 null
。要验证存储是否包含条目,您可以调用 StorageManager::has($identifier)
。
if ($manager->has(1)) { $model = $manager->find(1); }
要始终获取模型实例,您可以调用 StorageManager::findOrCreate($identifier)
。如果未找到条目,则会创建新的模型实例。需要实现 CreateAwareInterface
的存储。创建的实例将不会在存储中,直到您调用 flush()
。
$model = $manager->findOrCreate(1);
要从存储中删除条目,您可以调用 StorageManager::remove($identifier, $model)
。
$manager->remove(1, $product);
使用 StorageManager::removeAll()
删除存储中的所有条目。
$models = $manager->removeAll();
使用 StorageManager::findAll()
可以从存储中检索所有条目。
$models = $manager->findAll();
使用 StorageManager::findMultiple(array $identifiers)
可以从存储中检索多个条目。
$models = $manager->findMultiple([1, 2]);
Doctrine ORM
标识符必须由存储组件处理。因此,doctrine ORM 必须使用 "NONE" 作为标识符生成策略
XML
<doctrine-mapping> <entity name="Product"> <id name="id" type="integer"> <generator strategy="NONE" /> </id> </entity> </doctrine-mapping>
YAML
Product: type: entity id: id: type: integer generator: strategy: NONE
注解
<?php class Product { /** * @Id * @GeneratedValue(strategy="NONE") */ protected $id = null; }
高级用法
复合标识符/键
标识符可以是整数、字符串或复合标识符(键)的数组。
$manager->flush(['product_id' => 1, 'variation_id' => 4], $model);
复合标识符必须遵循上面示例的格式。
存储链
使用 StorageChain
同时在多个存储中保存条目。
$storageInMemory = new ArrayStorage(Product::class); $storageSession = new NativeSessionStorage(Product::class); $storageChain = new StorageChain(); $storageChain->register('product_default', $storageInMemory); $storageChain->register('product_session', $storageSession);
StorageChain
提供所有基本的 CRUD 操作 has()
、find()
、flush()
、remove()
。除了 create()
和 findOrCreate()
这两个方法不受支持。
存储工厂
要管理多个存储和/或不同模型,可以使用 StorageFactory
。
$storage = new ArrayStorage(Product::class); $storageFactory = new StorageFactory(); $storageFactory->register('product', $storage); $storage = $storageFactory->getStorage('product');
结合一切
因为实现与具体存储相同的StorageInterface
的StorageManager
和StorageChain
,所以您可以将它们组合在一起。
$storageInMemory = new ArrayStorage(Product::class); $storageSession = new NativeSessionStorage(Product::class); $storageChain = new StorageChain(); $storageChain->register('product_default', $storageInMemory); $storageChain->register('product_session', $storageSession); $storageManager = new StorageManager($storageChain); $storageFactory = new StorageFactory(); $storageFactory->register('product', $storageManager);
示例
启动内置的web服务器以查看示例操作
cd examples
php -S 127.0.0.1:3000
在浏览器中打开url
http://127.0.0.1:3000/index.html
有关 doctrine 示例,请参阅位于 example/doctrine
目录下的 README.md。
许可证
MIT许可证 (MIT)
版权所有 © 2015 Sven Sanzenbacher
在此特此授予任何获得此软件及其相关文档副本(“软件”)的人士,无限制地使用、复制、修改、合并、发布、分发、再许可和/或出售软件副本的权利,并允许向获得软件的人士授予上述权利,但需遵守以下条件
上述版权声明和本许可声明应包含在软件的所有副本或主要部分中。
软件按“现状”提供,不提供任何明示或暗示的保证,包括但不限于适销性、特定用途适用性和非侵权性保证。在任何情况下,作者或版权所有者均不对任何索赔、损害或其他责任负责,无论该责任是因合同行为、侵权行为或其他原因而引起的,也不论该责任是否与软件或软件的使用或其他交易有关。