Yes, you serialize it to an array explicitly and then later use "instantiateEntity". This instantiateEntity is the same dehydrate method used to turn a key/value set into an entity.Are there any other built in alternatives? I want to cache the finder result object so I can apply its methods when needed for each user.
public function getRatingTypeCacheData()
{
$ratingTypes = $this->finder('SV\ContentRatings:RatingType')
->order(['display_order', 'title'])
->whereOr(['visible' => 1], ['usable' => 1])
->fetch();
$cache = [];
foreach ($ratingTypes as $ratingTypeId => $ratingType)
{
// cache the entire object, as we re-hydrate into an entity later and don't want to accidentally wipe disabled settings
$cache[$ratingTypeId] = $ratingType->toArray();
}
return $cache;
}
public function getRatingTypesAsEntities()
{
// this can be called multiple times per rateable content being displayed, so keep a local cache
if ($this->_ratingEntities === null)
{
$ratingEntities = [];
$ratingTypes = $this->getRatingTypes();
foreach($ratingTypes as $ratingTypeId => $ratingType)
{
$ratingEntities[$ratingTypeId] = $this->em->instantiateEntity('SV\ContentRatings:RatingType', $ratingType);
}
$this->_ratingEntities = $ratingEntities;
}
return $this->_ratingEntities;
}
I assume you mean when it hits the true path here?Also; instantiateEntity will allocate the Entity object even if it doesn't use it due to an existing cached entity. Do you want me to report that as a bug
if (isset($this->entities[$class][$primary]))
XF2 unconditionally create the entity, if values are passed in then check to see if it exists in the cache.If so, then no, that would be expected. There should only ever be one instance of a given entity at a time (at least that's attached). This means that if changes happen (or are pending), they are reflected in all places that reference that entity (given PHP's passing object by reference), including any new instantiation requests.
What about storing multi-dimensional array in simpleCache? Would that be a problem?
We use essential cookies to make this site work, and optional cookies to enhance your experience.