battlecook/datacooker

这是一个用于不同类型数据存储的项目。它通过抽象单个库(pdo、Redis、Memcached等)提供了访问数据存储的接口。

dev-master 2020-02-24 12:23 UTC

This package is auto-updated.

Last update: 2024-09-24 22:32:57 UTC


README

这是什么?

这是一个用于不同种类数据存储的项目。

它通过抽象单个库(pdo、Redis、Memcached等)提供了访问数据存储的接口。

支持的数据存储

关系型数据库:Pdo管理的数据库

键值存储:memcached、redis、apcu

其他语言版本阅读:韩语

如何安装

composer require battlecook/datacooker:dev-master

如何使用

数据库模式

create table Item
(
	id1 int auto_increment,
	id2 int not null,
	id3 int not null,
	attr1 int not null,
	attr2 int not null,
	attr3 int not null,
	constraint Item_pk
		primary key (id1)
);

类定义

final class Item
{
    /**
     * @dataCookerAutoIncrement
     * @dataCookerIdentifier
     */
    public $id1;
    
    /**
     * @dataCookerIdentifier
     */
    public $id2;
    
    /**
     * @dataCookerIdentifier
     */
    public $id3;
    
    /**
     * @dataCookerAttribute
     */
    public $attr1;
    
    /**
     * @dataCookerAttribute
     */
    public $attr2;
    
    /**
     * @dataCookerAttribute
     */
    public $attr3;
}

你可以如下使用。

类中的注解代表数据属性。

有三种属性需要表示。

  • @dataCookerIdentifier:必需的。代表复杂唯一标识符(必须声明多次。)

  • @dataCookerAttribute:必需的。代表属性(必须声明多次。)

  • @dataCookerAutoIncrement:可选的。代表自增值。

DataStore提供了六个接口(get、search、set、add、remove、commit)

$store = new RelationDatabase([
            'store' => null,
            'hosts' => [
                [
                    'ip' => 'localhost',
                    'port' => 3306,
                    'dbname' => 'DataCooker',
                    'user' => 'root',
                    'password' => 'password'
                ]
            ]
        ]);
                         
$object = new Item();
$object->id1 = 1;
$object->id2 = 1;
$object->id3 = 1;
$object->attr1 = 1;
$object->attr2 = 1;
$object->attr3 = 1;

$ret = $store->get($object);
$ret = $store->search($object): array;
$ret = $store->set($object);
$ret = $store->add($object);
$ret = $store->remove($object);
$ret = $store->commit($data = null);

复杂DataStore示例(Memcached和关系型数据库)

之前
status in database
  
+-----+-----+-----+-------+-------+-------+
| id1 | id2 | id3 | attr1 | attr2 | attr3 |
+-----+-----+-----+-------+-------+-------+
|  1  |  1  |  1  |   1   |   1   |   1   |
+-----+-----+-----+-------+-------+-------+

status in memcached

                        value
                        
            id1        id2        id3   attribute
key => array(1 => array(1 => array(1 => array(1,1,1))))

进度
$store =  new Memcached(['store' => new RelationDatabase(['store' => null,
                                                          'hosts' => [['ip' => 'localhost', 'port' => 3306, 'dbname' => 'DataCooker', 'user' => 'root', 'password' => 'password']]]),
                         'hosts' => [['ip' => 'localhost', 'port' => 11211]]])
             
$object = new Item();
$object->id1 = 1;
$object->id2 = 1;
$object->id3 = 2;
$object->attr1 = 1;
$object->attr2 = 1;
$object->attr3 = 1;

$ret = $store->add($object);
之后
status in database
  
+-----+-----+-----+-------+-------+-------+
| id1 | id2 | id3 | attr1 | attr2 | attr3 |
+-----+-----+-----+-------+-------+-------+
|  1  |  1  |  1  |   1   |   1   |   1   |
+-----+-----+-----+-------+-------+-------+
|  1  |  1  |  2  |   1   |   1   |   1   |
+-----+-----+-----+-------+-------+-------+

status in memcached

                        value
                        
            id1        id2        id3   attribute
key => array(1 => array(1 => array(1 => array(1,1,1)
                                   2 => array(1,1,1))))

缓冲数据存储

BufferedDataStore基本上存储在PHP内存中。

如果你使用多个DataStore与BufferedDataStore一起,操作会有所不同。

当BufferedDataStore操作函数(get、set、add、remove)时,第一次它会从另一个存储库获取数据并将其加载到PHP内存中。

之后,它只在PHP内存中工作,直到调用commit()函数。

因此,如果你想从另一个DataStore应用,你必须调用commit函数。

如果你使用BufferedDataStore并且你的类中定义了@dataCookerAutoIncrement,

add函数首先执行以获取自增值的增加。

如果没有定义,它就像其他任何函数一样后处理。

当你使用Buffered DataStore时,你可以像事务一样执行多个数据存储。

之前
before status in database
  
+-----+-----+-----+-------+-------+-------+
| id1 | id2 | id3 | attr1 | attr2 | attr3 |
+-----+-----+-----+-------+-------+-------+
|  1  |  1  |  1  |   1   |   1   |   1   |
+-----+-----+-----+-------+-------+-------+
进度1
$store =  new Buffered(['store' => new RelationDatabase(['store' => null,
                        'hosts' => [['ip' => 'localhost', 'port' => 3306, 'dbname' => 'DataCooker', 'user' => 'root', 'password' => 'password']]]),
                         );       
$object = new Item();
$object->id1 = 1;
$object->id2 = 1;
$object->id3 = 1;
$object->attr1 = 2;
$object->attr2 = 2;
$object->attr3 = 2;

$ret = $store->set($object);
之后1
after status in database
  
+-----+-----+-----+-------+-------+-------+
| id1 | id2 | id3 | attr1 | attr2 | attr3 |
+-----+-----+-----+-------+-------+-------+
|  1  |  1  |  1  |   1   |   1   |   1   |
+-----+-----+-----+-------+-------+-------+
进度2
$store->commitAll();
之后2
after status in database
  
+-----+-----+-----+-------+-------+-------+
| id1 | id2 | id3 | attr1 | attr2 | attr3 |
+-----+-----+-----+-------+-------+-------+
|  1  |  1  |  1  |   2   |   2   |   2   |
+-----+-----+-----+-------+-------+-------+

许可证

DataCooker采用MIT许可证