Skip to content

Commit 506aa54

Browse files
committed
update cache routine and tests, create folder with method if none
1 parent 8747fb9 commit 506aa54

File tree

3 files changed

+57
-18
lines changed

3 files changed

+57
-18
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@ build/logs/clover.xml
1414
*.key
1515
test.php
1616
build
17+
tmp

shared/ez_sql_core.php

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,38 @@ class ezSQLcore extends ezQuery
2424
protected $show_errors = true;
2525
protected $num_queries = 0;
2626
protected $conn_queries = 0;
27+
2728
protected $captured_errors = array();
28-
protected $cache_dir = false;
29+
30+
31+
// Specify a cache dir. Path is taken from calling script
32+
protected $cache_dir = 'tmp'.\_DS.'ez_cache';
33+
34+
/**
35+
* if you want to cache EVERYTHING just do..
36+
*
37+
* $use_disk_cache = true;
38+
* $cache_queries = true;
39+
* $cache_timeout = 24;
40+
*/
2941
protected $cache_queries = false;
3042
protected $cache_inserts = false;
3143
protected $use_disk_cache = false;
32-
protected $cache_timeout = 24; // hours
44+
45+
// Cache expiry
46+
protected $cache_timeout = 24; // Note: this is hours
47+
3348
protected $db_connect_time = 0;
3449
protected $sql_log_file = false;
3550
protected $profile_times = array();
3651
protected $insert_id = null;
3752

53+
/**
54+
* Log number of rows the query returned
55+
* @var int Default is null
56+
*/
57+
protected $num_rows = null;
58+
3859
public $last_query = null;
3960
public $last_error = null;
4061
public $col_info = null;
@@ -90,7 +111,7 @@ class ezSQLcore extends ezQuery
90111
*/
91112
public function __construct()
92113
{
93-
parent::__construct();
114+
parent::__construct();
94115
}
95116

96117
/**
@@ -311,19 +332,31 @@ public function get_col_info($info_type = "name", $col_offset = -1)
311332
}
312333
}
313334

335+
/**
336+
* create cache directory if doesn't exists
337+
*/
338+
public function create_cache(string $path = null)
339+
{
340+
$cache_dir = empty($path) ? $this->cache_dir : $path;
341+
if ( ! \is_dir($cache_dir) ) {
342+
$this->cache_dir = $cache_dir;
343+
@\mkdir($cache_dir, ('\\' == \DIRECTORY_SEPARATOR ? null : 0755), true);
344+
}
345+
}
346+
314347
/**
315348
* store_cache
316349
*/
317350
public function store_cache(string $query, $is_insert)
318351
{
319352
// The would be cache file for this query
320-
$cache_file = $this->cache_dir.'/'.\md5($query);
353+
$cache_file = $this->cache_dir.\_DS.\md5($query);
321354

322355
// disk caching of queries
323356
if ( $this->use_disk_cache
324-
&& ( $this->cache_queries && ! $is_insert )
325-
|| ( $this->cache_inserts && $is_insert )
357+
&& ( $this->cache_queries && ! $is_insert ) || ( $this->cache_inserts && $is_insert )
326358
) {
359+
$this->create_cache();
327360
if ( ! \is_dir($this->cache_dir) ) {
328361
$this->register_error("Could not open cache dir: $this->cache_dir");
329362
$this->show_errors ? \trigger_error("Could not open cache dir: $this->cache_dir", \E_USER_WARNING) : null;
@@ -338,7 +371,7 @@ public function store_cache(string $query, $is_insert)
338371

339372
\file_put_contents($cache_file, \serialize($result_cache));
340373
if( \file_exists($cache_file . ".updating") )
341-
\unlink($cache_file . ".updating");
374+
\unlink($cache_file . ".updating");
342375
}
343376
}
344377
}
@@ -349,14 +382,14 @@ public function store_cache(string $query, $is_insert)
349382
public function get_cache(string $query)
350383
{
351384
// The would be cache file for this query
352-
$cache_file = $this->cache_dir.'/'.\md5($query);
385+
$cache_file = $this->cache_dir.\_DS.\md5($query);
353386

354387
// Try to get previously cached version
355388
if ( $this->use_disk_cache && \file_exists($cache_file) ) {
356389
// Only use this cache file if less than 'cache_timeout' (hours)
357390
if ( (\time() - \filemtime($cache_file)) > ($this->cache_timeout*3600)
358-
&& !(\file_exists($cache_file . ".updating")
359-
&& (\time() - \filemtime($cache_file . ".updating") < 60))
391+
&& !(\file_exists($cache_file . ".updating")
392+
&& (\time() - \filemtime($cache_file . ".updating") < 60))
360393
) {
361394
\touch($cache_file . ".updating"); // Show that we in the process of updating the cache
362395
} else {
@@ -370,7 +403,6 @@ public function get_cache(string $query)
370403

371404
// If debug ALL queries
372405
$this->trace || $this->debug_all ? $this->debug() : null ;
373-
374406
return $result_cache['return_value'];
375407
}
376408
}

tests/shared/ezSQLcoreTest.php

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -195,21 +195,27 @@ public function testGet_col_info() {
195195
*/
196196
public function testStore_cache() {
197197
$sql = 'SELECT * FROM ez_test';
198+
$this->object->setCache_Timeout(1);
199+
$this->object->setUse_Disk_Cache(true);
200+
$this->object->setCache_Queries(true);
201+
$this->object->setNum_Rows(5);
202+
$this->object->store_cache($sql, false);
198203

199-
$this->object->store_cache($sql, true);
200-
201-
$this->assertNull($this->object->get_cache($sql));
204+
$this->assertEquals(5, $this->object->get_cache($sql));
202205
} // testStore_cache
203206

204207
/**
205208
* @covers ezSQLcore::get_cache
206209
*/
207210
public function testGet_cache() {
208211
$sql = 'SELECT * FROM ez_test';
209-
210-
$this->object->store_cache($sql, true);
211-
212-
$this->assertNull($this->object->get_cache($sql));
212+
$this->object->setCache_Timeout(1);
213+
$this->object->setUse_Disk_Cache(true);
214+
$this->object->setCache_Queries(true);
215+
$this->object->setNum_Rows(2);
216+
$this->object->store_cache($sql, false);
217+
218+
$this->assertEquals(2, $this->object->get_cache($sql));
213219
} // testGet_cache
214220

215221
/**

0 commit comments

Comments
 (0)