mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-12 22:14:03 +00:00
Redis storage json encoding
This commit is contained in:
parent
c20942cda5
commit
b02126e8d7
1 changed files with 26 additions and 14 deletions
|
|
@ -44,20 +44,13 @@ class RedisStorageDriver implements StorageDriver
|
||||||
return $this->find($id);
|
return $this->find($id);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public function find(string $id): Tenant
|
||||||
* Get information about the tenant based on his id.
|
|
||||||
*
|
|
||||||
* @param string $id
|
|
||||||
* @param string[] $fields
|
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
public function find(string $id, array $fields = []): array
|
|
||||||
{
|
{
|
||||||
if (! $fields) {
|
if (! $fields) {
|
||||||
return $this->redis->hgetall("tenants:$id");
|
return $this->redis->hgetall("tenants:$id");
|
||||||
}
|
}
|
||||||
|
|
||||||
return array_combine($fields, $this->redis->hmget("tenants:$id", $fields));
|
return array_combine($fields, $this->redis->hmget("tenants:$id", $fields)); // todo factory
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getTenantIdByDomain(string $domain): ?string
|
public function getTenantIdByDomain(string $domain): ?string
|
||||||
|
|
@ -78,8 +71,15 @@ class RedisStorageDriver implements StorageDriver
|
||||||
|
|
||||||
public function updateTenant(Tenant $tenant): void
|
public function updateTenant(Tenant $tenant): void
|
||||||
{
|
{
|
||||||
$this->redis->hmset("tenants:{$tenant->id}", $tenant->data);
|
$this->redis->pipeline(function ($pipe) use ($tenant) {
|
||||||
// todo update domains
|
$this->redis->hmset("tenants:{$tenant->id}", $tenant->data);
|
||||||
|
|
||||||
|
foreach ($tenant->domains as $domain) {
|
||||||
|
$this->redis->hmset("domains:$domain", 'tenant_id', $tenant->id);
|
||||||
|
}
|
||||||
|
|
||||||
|
// todo deleted domains
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public function deleteTenant(Tenant $tenant): void
|
public function deleteTenant(Tenant $tenant): void
|
||||||
|
|
@ -95,7 +95,7 @@ class RedisStorageDriver implements StorageDriver
|
||||||
|
|
||||||
public function all(array $ids = []): array
|
public function all(array $ids = []): array
|
||||||
{
|
{
|
||||||
// todo $this->redis->pipeline()
|
// todo $this->redis->pipeline() - return?
|
||||||
$hashes = array_map(function ($hash) {
|
$hashes = array_map(function ($hash) {
|
||||||
return "tenants:{$hash}";
|
return "tenants:{$hash}";
|
||||||
}, $ids);
|
}, $ids);
|
||||||
|
|
@ -127,13 +127,20 @@ class RedisStorageDriver implements StorageDriver
|
||||||
public function getMany(array $keys, Tenant $tenant = null): array
|
public function getMany(array $keys, Tenant $tenant = null): array
|
||||||
{
|
{
|
||||||
$tenant = $tenant ?? $this->tenant();
|
$tenant = $tenant ?? $this->tenant();
|
||||||
return $this->redis->hmget("tenants:{$tenant->id}", $keys);
|
|
||||||
|
$result = [];
|
||||||
|
$values = $this->redis->hmget("tenants:{$tenant->id}", $keys);
|
||||||
|
foreach ($keys as $i => $key) {
|
||||||
|
$result[$key] = $values[$i];
|
||||||
|
}
|
||||||
|
|
||||||
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function put(string $key, $value, Tenant $tenant = null)
|
public function put(string $key, $value, Tenant $tenant = null)
|
||||||
{
|
{
|
||||||
$tenant = $tenant ?? $this->tenant();
|
$tenant = $tenant ?? $this->tenant();
|
||||||
$this->redis->hset("tenants:{$tenant->id}", $key, $value);
|
$this->redis->hset("tenants:{$tenant->id}", $key, json_encode($value));
|
||||||
|
|
||||||
return $value;
|
return $value;
|
||||||
}
|
}
|
||||||
|
|
@ -141,6 +148,11 @@ class RedisStorageDriver implements StorageDriver
|
||||||
public function putMany(array $kvPairs, Tenant $tenant = null): array
|
public function putMany(array $kvPairs, Tenant $tenant = null): array
|
||||||
{
|
{
|
||||||
$tenant = $tenant ?? $this->tenant();
|
$tenant = $tenant ?? $this->tenant();
|
||||||
|
|
||||||
|
foreach ($kvPairs as $key => $value) {
|
||||||
|
$kvPairs[$key] = json_encode($value);
|
||||||
|
}
|
||||||
|
|
||||||
$this->redis->hmset("tenants:{$tenant->id}", $kvPairs);
|
$this->redis->hmset("tenants:{$tenant->id}", $kvPairs);
|
||||||
|
|
||||||
return $kvPairs;
|
return $kvPairs;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue