stefangabos/Zebra_Database

Issue with cache (tested on disk and session)

Closed this issue · 4 comments

I have this error

First load: (no cache yet) - page will display data

Subsequent: (cached) - Warning: Invalid argument supplied for foreach() in /www/wwwroot/xxxxxx/xxxxx.php on line 71

Debug shows that it has cached the query, whether disk or session . But it wont render in foreach, print_r result is 0.

Im using Zebra_Session with Zebra_Database $link

// Retrieve threads from the database
#$threads = $db->query('SELECT * FROM threads ORDER BY timestamp DESC LIMIT ' . $offset . ', ' . $itemsPerPage, 300);
$threads = $db->select(
    '*',
    'threads',
    '',
    '',
    'timestamp DESC',
    "{$offset}, {$itemsPerPage}",
	300
);
    <?php foreach ($threads as $thread) : ?>
      ...
     <p>Timestamp: <?php echo date('Y-m-d H:i:s', $thread['timestamp']); ?></p>
     <p><?php echo $thread['content']; ?></p>
     ...
     <?php endforeach; ?>

Its been a while i not used ZDB maybe i have error in the cache syntax in queries.

i can't reproduce the error. here's what i am doing

$db->query('
    SELECT
        Name
    FROM
        city
    WHERE
        CountryCode = ?
    ORDER BY
        Name
', array($country_code), 300);

while ($row = $db->fetch_assoc()) {
    print_r('<pre>');
    print_r($row);
    print_r('</pre>');
}

...actually, looking at your code, in the commented out example, the one where you are using query, 300 is not in the correct position - it should be 3rd but it is second; see docs
in your example using select, this {$offset}, {$itemsPerPage} makes no sense as it should rather be [$offset, $itemsPerPage]; again, see docs

...actually, looking at your code, in the commented out example, the one where you are using query, 300 is not in the correct position - it should be 3rd but it is second; see docs in your example using select, this {$offset}, {$itemsPerPage} makes no sense as it should rather be [$offset, $itemsPerPage]; again, see docs

regarding the commented out query() - my bad, i updated it the last moment, yes it is indeed on the 3rd place. moving to my issue:

Issue 1: foreach() cannot handle cached data

Database
image

query()
image

foreach()
image

First run - "not yet cached"
image

First log - "not yet cached"
image

Subsequent run - "cached"
image

Subsequent run log - "cached"
image

Comment: The foreach() error only appear if i use cache. As of posting i will be using while() but i hope you can look into that also. Cache works on while() but not on foreach()

Issue 2: select() as per your suggestion to use [$offset, $itemsPerPage]:

image

First run:
image

Comment: If i use {$offset}, {$itemsPerPage} (currently using) it will run OK.

Issue 3: Cache works only using session

I am using /tmp/ and that path works with other php library.
image

Comment: using disk will throw: Could not cache query. Make sure path exists and is writable... to use disk i need to place the cache folder in the directory of Zebra_Database class itself

using php 7.4 on dev and live

@stefangabos thanks

hey, did you manage to get this working?