Skip to content

Commit

Permalink
cache tag support for mongo & jig mapper, ref #166
Browse files Browse the repository at this point in the history
  • Loading branch information
ikkez committed Dec 24, 2018
1 parent 021e32e commit 515b3d1
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
9 changes: 6 additions & 3 deletions db/jig/mapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ function($expr) {
* @return static[]|FALSE
* @param $filter array
* @param $options array
* @param $ttl int
* @param $ttl int|array
* @param $log bool
**/
function find($filter=NULL,array $options=NULL,$ttl=0,$log=TRUE) {
Expand All @@ -170,9 +170,12 @@ function find($filter=NULL,array $options=NULL,$ttl=0,$log=TRUE) {
$db=$this->db;
$now=microtime(TRUE);
$data=[];
$tag='';
if (is_array($ttl))
list($ttl,$tag)=$ttl;
if (!$fw->CACHE || !$ttl || !($cached=$cache->exists(
$hash=$fw->hash($this->db->dir().
$fw->stringify([$filter,$options])).'.jig',$data)) ||
$fw->stringify([$filter,$options])).($tag?'.'.$tag:'').'.jig',$data)) ||
$cached[0]+$ttl<microtime(TRUE)) {
$data=$db->read($this->file);
if (is_null($data))
Expand Down Expand Up @@ -347,7 +350,7 @@ function reduce($key,$handler,$finalize=null){
* @return int
* @param $filter array
* @param $options array
* @param $ttl int
* @param $ttl int|array
**/
function count($filter=NULL,array $options=NULL,$ttl=0) {
$now=microtime(TRUE);
Expand Down
16 changes: 11 additions & 5 deletions db/mongo/mapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ function cast($obj=NULL) {
* @param $fields string
* @param $filter array
* @param $options array
* @param $ttl int
* @param $ttl int|array
**/
function select($fields=NULL,$filter=NULL,array $options=NULL,$ttl=0) {
if (!$options)
Expand All @@ -130,10 +130,13 @@ function select($fields=NULL,$filter=NULL,array $options=NULL,$ttl=0) {
'limit'=>0,
'offset'=>0
];
$tag='';
if (is_array($ttl))
list($ttl,$tag)=$ttl;
$fw=\Base::instance();
$cache=\Cache::instance();
if (!($cached=$cache->exists($hash=$fw->hash($this->db->dsn().
$fw->stringify([$fields,$filter,$options])).'.mongo',
$fw->stringify([$fields,$filter,$options])).($tag?'.'.$tag:'').'.mongo',
$result)) || !$ttl || $cached[0]+$ttl<microtime(TRUE)) {
if ($options['group']) {
$grp=$this->collection->group(
Expand Down Expand Up @@ -194,7 +197,7 @@ function select($fields=NULL,$filter=NULL,array $options=NULL,$ttl=0) {
* @return static[]
* @param $filter array
* @param $options array
* @param $ttl int
* @param $ttl int|array
**/
function find($filter=NULL,array $options=NULL,$ttl=0) {
if (!$options)
Expand All @@ -213,13 +216,16 @@ function find($filter=NULL,array $options=NULL,$ttl=0) {
* @return int
* @param $filter array
* @param $options array
* @param $ttl int
* @param $ttl int|array
**/
function count($filter=NULL,array $options=NULL,$ttl=0) {
$fw=\Base::instance();
$cache=\Cache::instance();
$tag='';
if (is_array($ttl))
list($ttl,$tag)=$ttl;
if (!($cached=$cache->exists($hash=$fw->hash($fw->stringify(
[$filter])).'.mongo',$result)) || !$ttl ||
[$filter])).($tag?'.'.$tag:'').'.mongo',$result)) || !$ttl ||
$cached[0]+$ttl<microtime(TRUE)) {
$result=$this->collection->count($filter?:[]);
if ($fw->CACHE && $ttl)
Expand Down

0 comments on commit 515b3d1

Please sign in to comment.