PHPSocialNetwork / phpfastcache

A high-performance backend cache system. It is intended for use in speeding up dynamic web applications by alleviating database load. Well implemented, it can drops the database load to almost nothing, yielding faster page load times for users, better resource utilization. It is simple yet powerful.
https://www.phpfastcache.com
MIT License
2.36k stars 452 forks source link

Storing and retrieving mysql result arrays using Psr16Adapter #899

Closed pjsgsy closed 1 year ago

pjsgsy commented 1 year ago

What's your question ?

Hello, I am trying to store and retrieve MySQL result arrays using Psr16Adapter. I thought this would be very simple, but...

I use the following simple code

if(!$Psr16Adapter->has($key)){ // Setter action - nothing in cache Logger::info("Cache miss..",[$key]);
$result = $conn->query($sql); $Psr16Adapter->set($key, $result, 60); }else{ // Getter action - data is in cache $result = $Psr16Adapter->get($key); Logger::info("Cache hit",[$key]);
}

The problem is, that on a cache HIT, $result does not is not in fact a valid MySQL array, as stored.
If I print_r the arrays, from the cache, and as retrieved by a MySQL query, I get this for the cached data ["mysqli_result Object\n(\n)\n"] And this for the mysql_query returned data ["mysqli_result Object\n(\n [current_field] => 0\n [field_count] => 7\n [lengths] => \n [num_rows] => 3\n [type] => 0\n)\n"]

So, data in, does not equal data out! I am clearly missing something. I thought objects and arrays can be stored and retrieved directly by the cache? I have searched for a couple hours, trying to find a simple example for this and can't find one. I would have thought it was one of the most common scenarios. Perhaps not.

I tried serializing and unserializing the array before and after storage, thinking that might help, but got the exact same result.

References (optional)

No response

Do you have anything more you want to share? (optional)

No response

github-actions[bot] commented 1 year ago

Hello curious contributor ! Since it seems to be your first contribution, make sure that you've been:

Geolim4 commented 1 year ago

Hello,

What cache backend are you using ?

I think that some object cannot be serialized, like MySQL object directly.

Geolim4 commented 1 year ago

https://stackoverflow.com/questions/13217299/serialize-and-then-unserialize-mysql-result-object

You can't serialize MySQL resource objects. You have to convert them to array before putting in cache.

pjsgsy commented 1 year ago

Ah, OK! That explains it, then. Shame. Thank you for pointing me to the source.