Skip to content
Merged
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
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
RUN \
apk update \
&& apk add --no-cache postgresql-libs postgresql-dev make automake autoconf gcc g++ \
&& pecl install mongodb \
&& docker-php-ext-enable mongodb \
&& pecl install mongodb redis \
&& docker-php-ext-enable mongodb redis \
&& docker-php-ext-install opcache pgsql pdo_mysql pdo_pgsql \
&& rm -rf /var/cache/apk/*

Expand Down
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ Some examples to help you get started.
use PDO;
use Utopia\Database\Database;
use Utopia\Database\Adapter\MariaDB;
use Utopia\Cache\Cache;
use Utopia\Cache\Adapter\None as NoCache;

$dbHost = 'mariadb';
$dbPort = '3306';
Expand All @@ -71,7 +73,9 @@ $pdo = new PDO("mysql:host={$dbHost};port={$dbPort};charset=utf8mb4", $dbUser, $
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
]);

$database = new Database(new MariaDB($pdo));
$cache = new Cache(new NoCache());

$database = new Database(new MariaDB($pdo), $cache);
$database->setNamespace('myscope');

$database->create(); // Creates a new schema named `myscope`
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"php": ">=7.1",
"ext-pdo": "*",
"utopia-php/framework": "0.*.*",
"utopia-php/cache": "0.3.*",
"utopia-php/cache": "0.4.0",
"mongodb/mongodb": "1.8.0"
},
"require-dev": {
Expand Down
14 changes: 7 additions & 7 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,11 @@ services:
MYSQL_PASSWORD: password
MYSQL_TCP_PORT: 3307

redis:
image: redis:6.0-alpine
container_name: redis
networks:
- database

networks:
database:
42 changes: 39 additions & 3 deletions src/Database/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Utopia\Database\Validator\Structure;
use Utopia\Database\Exception\Authorization as AuthorizationException;
use Utopia\Database\Exception\Structure as StructureException;
use Utopia\Cache\Cache;

class Database
{
Expand Down Expand Up @@ -36,11 +37,19 @@ class Database
// Collections
const COLLECTIONS = 'collections';

// Cache
const TTL = 60 * 60 * 24; // 24 hours

/**
* @var Adapter
*/
protected $adapter;

/**
* @var Cache
*/
protected $cache;

/**
* Parent Collection
* Defines the structure for both system and custom collections
Expand Down Expand Up @@ -90,10 +99,12 @@ class Database

/**
* @param Adapter $adapter
* @param Cache $cache
*/
public function __construct(Adapter $adapter)
public function __construct(Adapter $adapter, Cache $cache)
{
$this->adapter = $adapter;
$this->cache = $cache;

self::addFilter('json',
function($value) {
Expand Down Expand Up @@ -440,7 +451,26 @@ public function getDocument(string $collection, string $id): Document
}

$collection = $this->getCollection($collection);
$document = $this->adapter->getDocument($collection->getId(), $id);
$document = null;
$cache = null;

// TODO@kodumbeats Check if returned cache id matches request
if ($cache = $this->cache->load($id, self::TTL)) {
$document = new Document($cache);
$validator = new Authorization($document, self::PERMISSION_READ);

if (!$validator->isValid($document->getRead()) && $collection->getId() !== self::COLLECTIONS) { // Check if user has read access to this document
return new Document();
}

if($document->isEmpty()) {
return $document;
}

return $document;
}

$document = $this->adapter->getDocument($collection->getId(), $id);

$document->setAttribute('$collection', $collection->getId());

Expand All @@ -457,6 +487,8 @@ public function getDocument(string $collection, string $id): Document
$document = $this->casting($collection, $document);
$document = $this->decode($collection, $document);

$this->cache->save($id, $document->getArrayCopy()); // save to cache after fetching from db

return $document;
}

Expand Down Expand Up @@ -544,9 +576,11 @@ public function updateDocument(string $collection, string $id, Document $documen
}

$document = $this->adapter->updateDocument($collection->getId(), $document);

$document = $this->decode($collection, $document);

$this->cache->purge($id);
$this->cache->save($document->getId(), $document->getArrayCopy());

return $document;
}

Expand All @@ -568,6 +602,8 @@ public function deleteDocument(string $collection, string $id): bool
throw new AuthorizationException($validator->getDescription());
}

$this->cache->purge($id);

return $this->adapter->deleteDocument($collection, $id);
}

Expand Down
26 changes: 13 additions & 13 deletions src/Database/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,19 @@ public function __construct(array $input = [])
throw new Exception('$write permission must be of type array');
}

foreach ($input as $key => &$value) {
if (\is_array($value)) {
if ((isset($value['$id']) || isset($value['$collection']))) {
$input[$key] = new self($value);
} else {
foreach ($value as $childKey => $child) {
if ((isset($child['$id']) || isset($child['$collection'])) && (!$child instanceof self)) {
$value[$childKey] = new self($child);
}
}
}
}
}
// foreach ($input as $key => &$value) {
// if (\is_array($value)) {
// if ((isset($value['$id']) || isset($value['$collection']))) {
// $input[$key] = new self($value);
// } else {
// foreach ($value as $childKey => $child) {
// if ((isset($child['$id']) || isset($child['$collection'])) && (!$child instanceof self)) {
// $value[$childKey] = new self($child);
// }
// }
// }
// }
// }

parent::__construct($input);
}
Expand Down
12 changes: 10 additions & 2 deletions tests/Database/Adapter/MariaDBTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
namespace Utopia\Tests\Adapter;

use PDO;
use Redis;
use Utopia\Database\Database;
use Utopia\Database\Adapter\MariaDB;
use Utopia\Cache\Cache;
use Utopia\Cache\Adapter\Redis as RedisAdapter;
use Utopia\Tests\Base;

class MariaDBTest extends Base
Expand All @@ -15,7 +18,7 @@ class MariaDBTest extends Base
static $database = null;

/**
* @reture Adapter
* @return Adapter
*/
static function getDatabase(): Database
{
Expand All @@ -36,7 +39,12 @@ static function getDatabase(): Database
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
]);

$database = new Database(new MariaDB($pdo));
$redis = new Redis();
$redis->connect('redis', 6379);
$redis->flushAll();
$cache = new Cache(new RedisAdapter($redis));

$database = new Database(new MariaDB($pdo), $cache);
$database->setNamespace('myapp_'.uniqid());

return self::$database = $database;
Expand Down