@@ -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 {
0 commit comments