Closed pjsgsy closed 1 year ago
Hello curious contributor ! Since it seems to be your first contribution, make sure that you've been:
Hello,
What cache backend are you using ?
I think that some object cannot be serialized, like MySQL object directly.
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.
Ah, OK! That explains it, then. Shame. Thank you for pointing me to the source.
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