Leaseweb / LswMemcacheBundle

Symfony bundle for Memcache Doctrine caching and session storage in the Web Debug Toolbar.
MIT License
202 stars 57 forks source link

FOS User / Extended entity result cache problem #55

Closed andrzejdziekonski closed 8 years ago

andrzejdziekonski commented 9 years ago

Hi, i am not sure where the problem is but i noticed that when i try to use extended fos user entity i retrieve from cache result which contains only entity base fields (my extended ones are null). I am doing it this way:

$cacheKey = 'blog_articles'
$result = $this->getCache($cacheKey);
        if($result == false){

$query = $em
                        ->createQueryBuilder()
                        ->select('partial a.{id,slug,title,lead,status,youtube_code,publish_at}, partial u.{id, firstname, lastname, image, slug}')
                        ->from('MyBundle:Article','a')
                        ->leftJoin('a.author','u')
                        [ ... ];

$result = $query->getQuery()->getResult();
$this->setCache($cacheKey, $result, 0, 7200);
}

where setCache and getCache are my methods to write/read cache to memcache pool. When i clear cache i see all the information about article author, but when i check saved cache fields from extended fos user entity (firstname, lastname, image, slug) are nulls (in my entity declaration property variables are 'protected'). Anything i do wrong?

I tried to add fos_user entity manager in doctrine.result_cache

result_cache:
            pool: myPoolId
            entity_manager: [default, fos_user.entity_manager]  # you may specify multiple entity_managers
            prefix: "result_"                # you may specify a prefix for the entries

but it didnt work. When i remove whole memcache doctrine configuration i also dont see those entity properties in cache.

andrzejdziekonski commented 9 years ago

Correct me if i am wrong: it is ok to cache whole object collection result (as above) with this bundle?

andrzejdziekonski commented 9 years ago

missclick with 'close'.

mevdschee commented 9 years ago

In order to store the data (in Memcache or any other cache) and retrieve it, it needs to be serialized and deserialized. This process kills some of the relations that the object has. If you want to test whether or not objects can sustain serialization and deserialization do (without caching):

$result = unserialize(serialize($query->getQuery()->getResult()));

If this does not work, you can not cache the data. Or to summarize:

Caching requires serialization and deserialization, if your data cannot sustain that, then you cannot cache it.

Hope that helps!

mevdschee commented 8 years ago

Closed due to inactivity