naucon/storage
Composer 安装命令:
composer require naucon/storage
包简介
Storage Abstraction Layer .
README 文档
README
About
This package is an abstraction layer for key/value Stores (storages). The goal is to decouple a concrete key/value store from the business logic.
Features
- storage manager as abstraction layer for storages or stores with basic CRUD operations.
- storage factory and registry to manage multiple storages or stores.
- chained storages to save entities in multiple storages or stores.
- support composite identifiers / keys.
Adapters
- array (in-memory)
- file system
- native session
- bridge to symfony session handler
- redis (predis) - requires redis 2.8 or higher
- doctrine ORM
- PSR-6 Cache
- PSR-16 Simple Cache
- null storage (stores nothing, no-memory)
Compatibility
- PHP 7.1 to 7.4
Installation
install the latest version via composer
composer require naucon/storage
Usage
Getting started
To use the storage we need a model (plain-old php objects). This model will be serialized so it is a good idea that the model implements the Serializable interface - even if it is not mandatory.
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; } }
With a defined model we are able to create a StorageManager. The StorageManager provides all the basic CRUD operations create(), has(), find(), flush(), remove().
To work it requires a storage provider. The provider implements a concrete storage or store. In this example we use the ArrayStorage but you can choose any other supported provider or build a custom storage to your needs.
use Naucon\Storage\Provider\ArrayStorage; use Naucon\Storage\StorageManager; $adapter = new ArrayStorage(Product::class); $manager = new StorageManager($adapter);
To save an entry in the storage we call flush($identifier, $model)
$model = new Product(); $model->setId(1); $model->setSku('U123'); $model->setDescription('Dragon fruit'); $manager->flush(1, $model);
To create a new instance of the model you can call StorageManager::create(). Requires a storage that implements CreateAwareInterface. The created instance will not be in the storage until you flush() it.
$model = $manager->create(); $model->setId(1); $model->setSku('U123'); $model->setDescription('Dragon fruit'); $manager->flush(1, $model);
To retrieve an entry from the storage we call StorageManager::find($identifier). If no entry was found you get null in return.
To verify if a storage contains an entry you can call StorageManager::has($identifier).
if ($manager->has(1)) { $model = $manager->find(1); }
To always get a model instance you can call StorageManager::findOrCreate($identifier). That will create a new model instance if no entry was found. Requires a storage that implements CreateAwareInterface.
The created instance will not be in the storage until you flush() it.
$model = $manager->findOrCreate(1);
To remove an entry from storage you can call StorageManager::remove($identifier, $model).
$manager->remove(1, $product);
Remove all entries from the storage with StorageManager::removeAll().
$models = $manager->removeAll();
With StorageManager::findAll() you can retrieve all entries from the storage.
$models = $manager->findAll();
With StorageManager::findMultiple(array $identifiers) you can retrieve multiple entries from the storage.
$models = $manager->findMultiple([1, 2]);
Doctrine ORM
The identifier must be handled by the storage component. Therefor doctrine ORM must use "NONE" as a Identifier Generation Strategies
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
Annotation
<?php class Product { /** * @Id * @GeneratedValue(strategy="NONE") */ protected $id = null; }
Advanced Usage
Composite Identifiers / keys
The Identifier can be an integer, string or array of composite identifiers (keys).
$manager->flush(['product_id' => 1, 'variation_id' => 4], $model);
A composite identifier has to follow the format of the upper example.
StorageChain
Use the StorageChain to save an entry in multiple storages at the same time.
$storageInMemory = new ArrayStorage(Product::class); $storageSession = new NativeSessionStorage(Product::class); $storageChain = new StorageChain(); $storageChain->register('product_default', $storageInMemory); $storageChain->register('product_session', $storageSession);
The StorageChain provides all the basic CRUD operations has(), find(), flush(), remove(). Except create() and findOrCreate() these two methods are not supported.
StorageFactory
To manage multiple storages and/or different models you can use the StorageFactory.
$storage = new ArrayStorage(Product::class); $storageFactory = new StorageFactory(); $storageFactory->register('product', $storage); $storage = $storageFactory->getStorage('product');
Combining Everything
Because the StorageManager and StorageChain implementing the same StorageInterface as the concrete storage provide you can combine them.
$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);
Example
Start the build-in webserver to see the examples in action:
cd examples
php -S 127.0.0.1:3000
open url in browser
http://127.0.0.1:3000/index.html
For the doctrine example please have a look at README.md in the example/doctrine directory.
License
The MIT License (MIT)
Copyright (c) 2015 Sven Sanzenbacher
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
naucon/storage 适用场景与选型建议
naucon/storage 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 4.6k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2024 年 05 月 24 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「abstraction」 「cache」 「storage」 「store」 「layer」 「key-value」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 naucon/storage 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 naucon/storage 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 naucon/storage 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A SDK for working with B2 cloud storage.
repository php library
Small library to access Microsoft Windows Azure Blob Storage with a Service or a StreamWrapper.
Powerful PHP database abstraction layer (DBAL) with many features for database schema introspection and management.
ADOdb is a PHP database abstraction layer library
Slimmed down concise interfaces and query builder for database queries and transactions which can be layered / decorated.
统计信息
- 总下载量: 4.6k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 2
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-05-24