Skip to content

Commit add0daa

Browse files
committed
update cache routine and tests, create folder with method if none
1 parent 6eef1b9 commit add0daa

File tree

3 files changed

+60
-25
lines changed

3 files changed

+60
-25
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@ test.php
1515
*.key
1616
*.crt
1717
*.cache
18+
tmp

lib/ezsqlModel.php

Lines changed: 44 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ class ezsqlModel extends ezQuery implements ezsqlModelInterface
4646
protected $conn_queries = 0;
4747
protected $captured_errors = array();
4848

49+
/**
50+
* Specify a cache dir. Path is taken from calling script
51+
* @var string
52+
*/
53+
protected $cache_dir = 'tmp'.\_DS.'ez_cache';
54+
4955
/**
5056
* Disk Cache Setup
5157
* (1. You must create this dir. first!)
@@ -57,13 +63,18 @@ class ezsqlModel extends ezQuery implements ezsqlModelInterface
5763
protected $use_disk_cache = false;
5864

5965
/**
60-
* Specify a cache dir. Path is taken from calling script
61-
* @var string
66+
* Cache expiry, this is hours
67+
* @var int
68+
*/
69+
protected $cache_timeout = 24;
70+
71+
/**
72+
* if you want to cache EVERYTHING just do..
73+
*
74+
* $use_disk_cache = true;
75+
* $cache_queries = true;
76+
* $cache_timeout = 24;
6277
*/
63-
protected $cache_dir = 'ez_cache';
64-
65-
// Cache expiry
66-
protected $cache_timeout = 24; // Note: this is hours
6778

6879
/**
6980
* By wrapping up queries you can ensure that the default
@@ -73,6 +84,12 @@ class ezsqlModel extends ezQuery implements ezsqlModelInterface
7384
protected $cache_queries = false;
7485
protected $cache_inserts = false;
7586

87+
/**
88+
* Log number of rows the query returned
89+
* @var int Default is null
90+
*/
91+
protected $num_rows = null;
92+
7693
protected $db_connect_time = 0;
7794
protected $sql_log_file = false;
7895
protected $profile_times = array();
@@ -359,20 +376,33 @@ public function get_col_info(string $info_type = "name", int $col_offset = -1)
359376
}
360377
}
361378
}
362-
379+
380+
/**
381+
* create cache directory if doesn't exists
382+
* @param string $path
383+
*/
384+
public function create_cache(string $path = null)
385+
{
386+
$cache_dir = empty($path) ? $this->cache_dir : $path;
387+
if ( ! \is_dir($cache_dir) ) {
388+
$this->cache_dir = $cache_dir;
389+
@\mkdir($cache_dir, ('\\' == \DIRECTORY_SEPARATOR ? null : 0755), true);
390+
}
391+
}
392+
363393
/**
364394
* store_cache
365395
*/
366396
public function store_cache(string $query, bool $is_insert)
367397
{
368398
// The would be cache file for this query
369-
$cache_file = $this->cache_dir.'/'.\md5($query);
399+
$cache_file = $this->cache_dir.\_DS.\md5($query);
370400

371401
// disk caching of queries
372402
if ( $this->use_disk_cache
373-
&& ( $this->cache_queries && ! $is_insert )
374-
|| ( $this->cache_inserts && $is_insert )
403+
&& ( $this->cache_queries && ! $is_insert ) || ( $this->cache_inserts && $is_insert )
375404
) {
405+
$this->create_cache();
376406
if ( ! \is_dir($this->cache_dir) ) {
377407
$this->register_error("Could not open cache dir: $this->cache_dir");
378408
$this->show_errors ? \trigger_error("Could not open cache dir: $this->cache_dir", \E_USER_WARNING) : null;
@@ -387,7 +417,7 @@ public function store_cache(string $query, bool $is_insert)
387417

388418
\file_put_contents($cache_file, \serialize($result_cache));
389419
if( \file_exists($cache_file . ".updating") )
390-
\unlink($cache_file . ".updating");
420+
\unlink($cache_file . ".updating");
391421
}
392422
}
393423
}
@@ -398,14 +428,14 @@ public function store_cache(string $query, bool $is_insert)
398428
public function get_cache(string $query)
399429
{
400430
// The would be cache file for this query
401-
$cache_file = $this->cache_dir.'/'.\md5($query);
431+
$cache_file = $this->cache_dir.\_DS.\md5($query);
402432

403433
// Try to get previously cached version
404434
if ( $this->use_disk_cache && \file_exists($cache_file) ) {
405435
// Only use this cache file if less than 'cache_timeout' (hours)
406436
if ( (\time() - \filemtime($cache_file)) > ($this->cache_timeout*3600)
407-
&& !(\file_exists($cache_file . ".updating")
408-
&& (\time() - \filemtime($cache_file . ".updating") < 60))
437+
&& !(\file_exists($cache_file . ".updating")
438+
&& (\time() - \filemtime($cache_file . ".updating") < 60))
409439
) {
410440
\touch($cache_file . ".updating"); // Show that we in the process of updating the cache
411441
} else {

tests/ezsqlModelTest.php

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -176,25 +176,29 @@ public function testGet_col_info()
176176
/**
177177
* @covers ezsql\ezsqlModel::store_cache
178178
*/
179-
public function testStore_cache()
180-
{
179+
public function testStore_cache() {
181180
$sql = 'SELECT * FROM ez_test';
181+
$this->object->setCache_Timeout(1);
182+
$this->object->setUse_Disk_Cache(true);
183+
$this->object->setCache_Queries(true);
184+
$this->object->setNum_Rows(5);
185+
$this->object->store_cache($sql, false);
182186

183-
$this->object->store_cache($sql, true);
184-
185-
$this->assertNull($this->object->get_cache($sql));
187+
$this->assertEquals(5, $this->object->get_cache($sql));
186188
} // testStore_cache
187189

188190
/**
189191
* @covers ezsql\ezsqlModel::get_cache
190192
*/
191-
public function testGet_cache()
192-
{
193+
public function testGet_cache() {
193194
$sql = 'SELECT * FROM ez_test';
194-
195-
$this->object->store_cache($sql, true);
196-
197-
$this->assertNull($this->object->get_cache($sql));
195+
$this->object->setCache_Timeout(1);
196+
$this->object->setUse_Disk_Cache(true);
197+
$this->object->setCache_Queries(true);
198+
$this->object->setNum_Rows(2);
199+
$this->object->store_cache($sql, false);
200+
201+
$this->assertEquals(2, $this->object->get_cache($sql));
198202
} // testGet_cache
199203

200204
/**

0 commit comments

Comments
 (0)