Skip to content

Commit

Permalink
add DB cache read/write control with custom keys
Browse files Browse the repository at this point in the history
  • Loading branch information
shayanderson committed Jun 14, 2017
1 parent ec2fe4e commit 2ff21cd
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 17 deletions.
5 changes: 3 additions & 2 deletions app/mod/IndexController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ class IndexController
*/
public function home()
{
view()->set('status', 'ready');
view('home');
view('home', [
'status' => 'ready'
]);
}
}
33 changes: 30 additions & 3 deletions app/vendor/Eco/System/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,11 @@ private function __hasConn($connection_id)
*/
private function __initCache(\Eco\Cache &$cache, $query, $params, $key_add = null)
{
// auto set cache key
$key = trim($query) . ( $params ? '~' . $key_add . implode('~', $params) : null );
$cache->key($cache->encodeKey($key), true); // set key
if(!$cache->hasKey()) // auto set cache key
{
$key = trim($query) . ( $params ? '~' . $key_add . implode('~', $params) : null );
$cache->key($cache->encodeKey($key), true); // set key
}

// set subdir for connection ID
$cache->path('ecodb' . DIRECTORY_SEPARATOR . $this->__conn_id);
Expand Down Expand Up @@ -397,6 +399,11 @@ public function get($table_or_sql, $params = null)
$this->__initCache($cache, $table_or_sql,
$this->__prepParams($param_index, func_get_args()));

if(isset($cache->db_query)) // stop
{
return $cache;
}

if($cache->has()) // cache exists
{
return $cache->get();
Expand Down Expand Up @@ -434,6 +441,11 @@ public function getAll($table_or_sql)
$this->__initCache($cache, $table_or_sql,
$this->__prepParams($param_index, func_get_args()));

if(isset($cache->db_query)) // stop
{
return $cache;
}

if($cache->has())
{
return $cache->get();
Expand Down Expand Up @@ -634,6 +646,11 @@ public function pagination($query, $params = null)
$this->__initCache($cache, $query, $this->__prepParams($param_index, func_get_args()),
'pg~');

if(isset($cache->db_query)) // stop
{
return $cache;
}

if($cache->has())
{
return $cache->get();
Expand Down Expand Up @@ -675,6 +692,11 @@ public function query($query, $params = null)

$this->__initCache($cache, $query, $this->__prepParams($param_index, func_get_args()));

if(isset($cache->db_query)) // stop
{
return $cache;
}

if($cache->has())
{
return $cache->get();
Expand Down Expand Up @@ -714,6 +736,11 @@ public function queryArrayParam($query, $params)

$this->__initCache($cache, $query, $params);

if(isset($cache->db_query)) // stop
{
return $cache;
}

if($cache->has())
{
return $cache->get();
Expand Down
2 changes: 1 addition & 1 deletion app/vendor/Eco/version.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@
/**
* Eco version
*/
const ECO_VERSION = '1.3.1';
const ECO_VERSION = '1.3.2';
18 changes: 7 additions & 11 deletions docs/cache.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,18 @@ The `\Eco\Cache` class can be used for server-side caching, for example:
$cache = new \Eco\Cache('article-14');

// check if cache exists
if(!$cache->has())
if($cache->has()) // cache exists
{
// cache does not exists
$data = 'the cache value';

// write cache
$cache->set($data);
$data = $cache->get();
}
else
else // cache does not exists
{
// cache exists
$data = $cache->get();
$data = 'the cache value';

$cache->set($data); // write cache
}

// output
echo $data;
echo $data; // output
```
All cache data is serialized by default so different PHP types can be cached, for example:
```php
Expand Down
22 changes: 22 additions & 0 deletions docs/database.md
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,28 @@ Caching can be used with these methods:
- [`queryArrayParam()`](#execute-a-query)
- [`pagination()`](#pagination)

Custom cache keys can be used, for example:
```php
$row = db()->get(new \Eco\Cache('table-1'), 'table WHERE x = ?', 1);
```

> It is possible to control a database cache without executing the cache read/write. For example, if you want to delete the cache without executing read/write it can be accomplished using:
```php
$cache = new \Eco\Cache;
// set property db_query to stop read/write
$cache->db_query = false;

// no read/write is executed in call (no DB load)
// will not return row(s):
db()->get($cache, 'table WHERE x = ?', 1);

// delete the cache with zero load on DB
$cache->delete();
// or get cache info with zero DB load
$key = $cache->getKey();
$path = $cache->getFilePath();
```


### Other Methods
The following methods are also available
Expand Down

0 comments on commit 2ff21cd

Please sign in to comment.