ytake/laravel-couchbase

Couchbase 提供者用于 Laravel

资助包维护!
ytake

1.1.0 2018-06-03 14:01 UTC

README

适用于 Laravel 5.1.* (更高版本)

缓存、会话、数据库、队列扩展包 需要 ext-couchbase 扩展

Build Status Code Coverage Scrutinizer Code Quality StyleCI

Packagist Packagist Packagist Codacy Badge

SensioLabsInsight

注意

支持自动发现、设计文档、缓存锁(Laravel5.5)

弃用

不推荐 couchbase-memcached 驱动 couchbase 会话驱动

安装

$ composer require ytake/laravel-couchbase

或配置文件 config/app.php

'providers' => [
    // added service provider
    \Ytake\LaravelCouchbase\CouchbaseServiceProvider::class,
    \Ytake\LaravelCouchbase\ConsoleServiceProvider::class,
]

使用方法

数据库扩展

添加数据库驱动(config/database.php)

'couchbase' => [
    'driver' => 'couchbase',
    'host' => 'couchbase://127.0.0.1',
    'user' => 'userName', // optional administrator
    'password' => 'password', // optional administrator
    // optional configuration / management operations against a bucket.
    'administrator' => [
        'user'     => 'Administrator',
        'password' => 'password',
    ],
],

集群案例

'couchbase' => [
    'driver' => 'couchbase',
    'host' => 'couchbase://127.0.0.1,192.168.1.2',
    'user' => 'userName', // optional administrator
    'password' => 'password', // optional administrator
],

选择 bucket table() 方法或

基本使用 bucket() 方法

N1QL 支持(启用 upsert)

参见 http://developer.couchbase.com/documentation/server/4.1/n1ql/n1ql-language-reference/index.html

SELECT

// service container access
$this->app['db']->connection('couchbase')
    ->table('testing')->where('whereKey', 'value')->first();

// use DB facades
\DB::connection('couchbase')
    ->table('testing')->where('whereKey', 'value')->get();

INSERT / UPSERT

$value = [
    'click' => 'to edit',
    'content' => 'testing'
];
$key = 'insert:and:delete';

$result = \DB::connection('couchbase')
    ->table('testing')->key($key)->insert($value);

\DB::connection('couchbase')
    ->table('testing')->key($key)->upsert([
        'click'   => 'to edit',
        'content' => 'testing for upsert',
    ]);

DELETE / UPDATE

\DB::connection('couchbase')
    ->table('testing')->key($key)->where('clicking', 'to edit')->delete();

\DB::connection('couchbase')
    ->table('testing')->key($key)
    ->where('click', 'to edit')->update(
        ['click' => 'testing edit']
    );
执行查询

示例)

"delete from testing USE KEYS "delete" RETURNING *"
"update testing USE KEYS "insert" set click = ? where click = ? RETURNING *"

返回

默认 *

\DB::connection('couchbase')
    ->table('testing')
    ->where('id', 1)
    ->offset($from)->limit($perPage)
    ->orderBy('created_at', $sort)
    ->returning(['id', 'name'])->get();

视图查询

$view = \DB::view("testing");
$result = $view->execute($view->from("dev_testing", "testing"));

缓存扩展

用于 couchbase 类型 bucket

config/cache.php

'couchbase' => [
   'driver' => 'couchbase',
   'bucket' => 'session'
],

用于 memcached 类型 bucket

'couchbase-memcached' => [
    'driver'  => 'couchbase-memcached',
    'servers' => [
        [
            'host' => '127.0.0.1',
            'port' => 11255,
            'weight' => 100,
            'bucket' => 'memcached-bucket-name',
            'option' => [
                // curl option
            ],
        ],
    ],
],

不支持

couchbase bucket,使用 bucket 密码

config/cache.php

'couchbase' => [
   'driver' => 'couchbase',
   'bucket' => 'session',
   'bucket_password' => 'your bucket password'
],

会话扩展

.env 等..

指定 couchbase 驱动

一致性

默认 :CouchbaseN1qlQuery::NOT_BOUNDED

$this->app['db']->connection('couchbase')
    ->consistency(\CouchbaseN1qlQuery::REQUEST_PLUS)
    ->table('testing')
    ->where('id', 1)
    ->returning(['id', 'name'])->get();

可调用一致性

$this->app['db']->connection('couchbase')
    ->callableConsistency(\CouchbaseN1qlQuery::REQUEST_PLUS, function ($con) {
        return $con->table('testing')->where('id', 1)->returning(['id', 'name'])->get();           
    });

事件

针对 N1QL

模式/迁移

数据库驱动还有限制性地支持模式构建器。
轻松操纵索引(主索引和辅助索引)

use Ytake\LaravelCouchbase\Schema\Blueprint as CouchbaseBlueprint;

\Schema::create('samples', function (CouchbaseBlueprint $table) {
    $table->primaryIndex(); // primary index
    $table->index(['message', 'identifier'], 'sample_secondary_index'); // secondary index
    // dropped
    $table->dropIndex('sample_secondary_index'); 
    $table->dropPrimary();
});

支持的操作

  • 创建和删除
  • 索引和删除索引(主索引和辅助索引)

Artisan

用于 couchbase 索引操作

-h 获取更多信息。

创建设计

config/couchbase.php

return [
    'design' => [
        'Your Design Document Name' => [
            'views' => [
                'Your View Name' => [
                    'map' => file_get_contents(__DIR__ . '/../resources/sample.ddoc'),
                ],
            ],
        ],
    ]
];

队列

更改 config/queue.php 中的驱动

    'connections' => [
        'couchbase' => [
            'driver' => 'couchbase',
            'bucket' => 'jobs',
            'queue' => 'default',
            'retry_after' => 90,
        ],
    ],

示例

php artisan queue:work couchbase --queue=send_email

黑客攻击

要运行测试,应在本地 Couchbase 集群上创建以下 bucket

$cluster = new CouchbaseCluster('couchbase://127.0.0.1');
$clusterManager = $cluster->manager('Administrator', 'password');
$clusterManager->createBucket('testing', ['bucketType' => 'couchbase', 'saslPassword' => '', 'flushEnabled' => true]);
$clusterManager->createBucket('memcache-couch', ['bucketType' => 'memcached', 'saslPassword' => '', 'flushEnabled' => true]);
sleep(5);
$bucketManager = $cluster->openBucket('testing')->manager();
$bucketManager->createN1qlPrimaryIndex();

此外,测试还期望在端口 11255 上监听常规 Memcached 守护进程。

很快

  • 认证驱动
  • Eloquent 支持

Couchbase 文档

REST API / 创建和编辑 bucket
couchbase-cli / user-manage
认证
授权 API