Skip to content

Replace deprecated SDK #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:

strategy:
matrix:
php: ['8.0', '8.1', '8.2', '8.3']
php: ['8.2', '8.3']
dependency-version: [prefer-lowest, prefer-stable]

name: PHP ${{ matrix.php }} - ${{ matrix.dependency-version }}
Expand Down Expand Up @@ -59,4 +59,3 @@ jobs:
ACS_ACCESS_KEY_SECRET: ${{ secrets.ACS_ACCESS_KEY_SECRET }}
TABLESTORE_ENDPOINT: ${{ secrets.TABLESTORE_ENDPOINT }}
TABLESTORE_TABLE: ${{ secrets.TABLESTORE_TABLE }}

13 changes: 9 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,16 @@
}
],
"require": {
"php": "^8.0",
"dew-serverless/tablestore-php": "^1.1"
"php": "^8.2",
"dew-serverless/acs-sdk-php": "^0.2.0",
"google/protobuf": "^4.0"
},
"require-dev": {
"orchestra/testbench": "^7.37|^8.17",
"laravel/pint": "^1.0",
"orchestra/testbench": "^7.0|^8.0|^9.0",
"php-http/guzzle7-adapter": "^1.0",
"phpstan/phpstan": "^1.10",
"phpstan/phpstan-strict-rules": "^1.5",
"laravel/pint": "^1.0",
"rector/rector": "^0.18.12"
},
"autoload": {
Expand Down Expand Up @@ -57,5 +59,8 @@
"@lint",
"@test:integration"
]
},
"config": {
"sort-packages": true
}
}
23 changes: 10 additions & 13 deletions src/TablestoreServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Dew\TablestoreDriver;

use Dew\Tablestore\Tablestore;
use Dew\Acs\Tablestore\TablestoreInstance;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\ServiceProvider;

Expand All @@ -29,18 +29,15 @@ public function register()
private function registerCacheDriver(): void
{
Cache::extend('tablestore', function ($app, $config) {
$client = new Tablestore(
$config['key'], $config['secret'],
$config['endpoint'], $config['instance'] ?? null
);

if (isset($config['token'])) {
$client->tokenUsing($config['token']);
}

if (isset($config['http'])) {
$client->optionsUsing($config['http']);
}
$client = new TablestoreInstance([
'credentials' => [
'key' => $config['key'],
'secret' => $config['secret'],
'token' => $config['token'] ?? null,
],
'instance' => $config['instance'] ?? null,
'endpoint' => $config['endpoint'],
]);

return Cache::repository(
new TablestoreStore(
Expand Down
63 changes: 34 additions & 29 deletions src/TablestoreStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@

namespace Dew\TablestoreDriver;

use Dew\Tablestore\Attribute;
use Dew\Tablestore\Exceptions\TablestoreException;
use Dew\Tablestore\PlainbufferWriter;
use Dew\Tablestore\PrimaryKey;
use Dew\Tablestore\Responses\RowDecodableResponse;
use Dew\Tablestore\Tablestore;
use Dew\Acs\Tablestore\Attribute;
use Dew\Acs\Tablestore\InstanceException;
use Dew\Acs\Tablestore\Messages\ComparatorType;
use Dew\Acs\Tablestore\Messages\Filter;
use Dew\Acs\Tablestore\Messages\FilterType;
use Dew\Acs\Tablestore\Messages\SingleColumnValueFilter;
use Dew\Acs\Tablestore\Plainbuf;
use Dew\Acs\Tablestore\PlainBufferWriter;
use Dew\Acs\Tablestore\PrimaryKey;
use Dew\Acs\Tablestore\TablestoreInstance;
use Illuminate\Contracts\Cache\LockProvider;
use Illuminate\Contracts\Cache\Store;
use Illuminate\Support\Carbon;
use Illuminate\Support\InteractsWithTime;
use InvalidArgumentException;
use Protos\ComparatorType;
use Protos\Filter;
use Protos\FilterType;
use Protos\SingleColumnValueFilter;
use RuntimeException;

final class TablestoreStore implements LockProvider, Store
Expand All @@ -34,7 +34,7 @@ final class TablestoreStore implements LockProvider, Store
* Create a Tablestore cache store.
*/
public function __construct(
protected Tablestore $tablestore,
protected TablestoreInstance $tablestore,
protected string $table,
protected string $keyAttribute = 'key',
protected string $valueAttribute = 'value',
Expand All @@ -52,16 +52,19 @@ public function __construct(
*/
public function get($key)
{
$item = $this->tablestore->table($this->table)
$buffer = $this->tablestore->table($this->table)
->whereKey($this->keyAttribute, $this->prefix.$key)
->where($this->expirationAttribute, '>', Carbon::now()->getTimestamp())
->get()->getDecodedRow();
->get()
->getRow();

if ($item === null) {
if ($buffer === '') {
return;
}

/** @var \Dew\Tablestore\Contracts\HasValue[] */
$item = Plainbuf::decode($buffer);

/** @var \Dew\Acs\Tablestore\Cells\HasValue[] */
$values = $item[$this->valueAttribute] ?? [];

return isset($values[0]) ? $this->unserialize($values[0]->value()) : null;
Expand Down Expand Up @@ -95,23 +98,25 @@ public function many(array $keys)

$result = array_fill_keys($keys, null);

/** @var \Protos\TableInBatchGetRowResponse[] */
/** @var \Dew\Acs\Tablestore\Messages\TableInBatchGetRowResponse[] */
$tables = $response->getTables();

/** @var \Protos\RowInBatchGetRowResponse[] */
/** @var \Dew\Acs\Tablestore\Messages\RowInBatchGetRowResponse[] */
$rows = $tables[0]->getRows();

foreach ($rows as $row) {
$item = (new RowDecodableResponse($row))->getDecodedRow();
$buffer = $row->getRow();

if ($item === null) {
if ($buffer === '') {
continue;
}

/** @var \Dew\Tablestore\Cells\StringPrimaryKey */
$item = Plainbuf::decode($buffer);

/** @var \Dew\Acs\Tablestore\Cells\StringPrimaryKey */
$key = $item[$this->keyAttribute];

/** @var \Dew\Tablestore\Contracts\HasValue[] */
/** @var \Dew\Acs\Tablestore\Cells\HasValue[] */
$values = $item[$this->valueAttribute] ?? [];

if (isset($values[0])) {
Expand Down Expand Up @@ -183,7 +188,7 @@ public function add($key, $value, $seconds)
{
try {
Attribute::integer($this->expirationAttribute, Carbon::now()->getTimestamp())
->toFormattedValue($now = new PlainbufferWriter);
->toFormattedValue($now = new PlainBufferWriter);

// Include only items that do not exist or that have expired
// expression: expiration <= now
Expand All @@ -205,8 +210,8 @@ public function add($key, $value, $seconds)
Attribute::createFromValue($this->valueAttribute, $this->serialize($value)),
Attribute::integer($this->expirationAttribute, $this->toTimestamp($seconds)),
]);
} catch (TablestoreException $e) {
if ($e->getError()->getCode() === 'OTSConditionCheckFail') {
} catch (InstanceException $e) {
if ($e->getError()?->getCode() === 'OTSConditionCheckFail') {
return false;
}

Expand Down Expand Up @@ -235,8 +240,8 @@ public function increment($key, $value = 1)
]);

return true;
} catch (TablestoreException $e) {
if ($e->getError()->getCode() === 'OTSConditionCheckFail') {
} catch (InstanceException $e) {
if ($e->getError()?->getCode() === 'OTSConditionCheckFail') {
return false;
}

Expand All @@ -263,8 +268,8 @@ public function decrement($key, $value = 1)
]);

return true;
} catch (TablestoreException $e) {
if ($e->getError()->getCode() === 'OTSConditionCheckFail') {
} catch (InstanceException $e) {
if ($e->getError()?->getCode() === 'OTSConditionCheckFail') {
return false;
}

Expand Down Expand Up @@ -399,7 +404,7 @@ private function toTimestamp(int $seconds): int
/**
* The underlying Tablestore client.
*/
public function getClient(): Tablestore
public function getClient(): TablestoreInstance
{
return $this->tablestore;
}
Expand Down
Loading