1

I use resources on api site to extend data(labels, formatting, related data ...).

But when I used ItemResource writing into redis :

$activeItems = Item::with('creator')->getByPublished(true)->get();
foreach( $activeItems as $item ) {
    $redisUniqueKey = CachingDataTypeEnum::cdtItem . '_' . $item->id;
    Redis::set($redisUniqueKey, serialize( new ItemResource($item->toArray())) );
    Redis::expire($redisUniqueKey, $itemsCachingHours * 3600 *100);
    $generatedItemsCount++;
}

Resulting item under redis has header like :

O:34:"App\Http\Resources\ItemResource":3:{s:8:"resource";a:10:{s:2:"id";i:4;s:5:"title";s:37:"Item C...

I dislike this ItemResource reference and wonder could it raise some problems in future using of these redis data ?

How it can be fixed ? "laravel/framework": "^9.47", "predis/predis": "^2.1",

dbf
  • 3,278
  • 1
  • 24
  • 34
Petro Gromovo
  • 1,755
  • 5
  • 33
  • 91
  • You mean the namespace magically changes during development? If that is the case, the cache has to be emptied anyway, so no, if the data is not persistent, it doesn’t matter. – dbf Feb 15 '23 at 13:41
  • Not sure that I understood you message. I dislike App\Http\Resources\ItemResource in redis item... – Petro Gromovo Feb 15 '23 at 15:29
  • Yes because of the namespace, as I said in the previous comment – dbf Feb 15 '23 at 19:29
  • Redis is being used here as a memory cache store right? Is it being used for persistent storage? – dbf Feb 15 '23 at 19:34
  • I have a task making with eloquent application with mysql used for data storage to make it also compatible with redis. I am new in redis - just search search how that can be done. – Petro Gromovo Feb 16 '23 at 04:31
  • 1
    Ok, so `redis` is used as a cache and `mysql` is used as the persistent data storage, e.g. database. The namespace `App\Http\Resources\ItemResource` in cache (`redis`) is not a problem, because during development and updating sources, you have to clear out the cache anyway. If not, and parts of the cache have be present, you have to use multiple connections set with different database parameters. I'm wondering why you are using the `Redis` class and not the `Cache` class to store the `Redis::set($redisUniqueKey)` part for example. – dbf Feb 16 '23 at 10:08
  • Are Cache class methods preferable in comparison with methods of Redis class ? – Petro Gromovo Feb 16 '23 at 11:28
  • 1
    Yes they are, please consult Laravels documentation about caching and database access – dbf Feb 16 '23 at 17:59

0 Answers0