Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
cache
  • Hello,

    In my code, I have a query in a loop.
    It's possible to have the same query several times.

    The number of query can be big, and duplicated query too.

    I tried to do a :

    try {
        $product_unit = \Cache::get('product_units.' . $id);
    } catch (\CacheNotFoundException $e) {
        $product_unit = \Model_Product_Unit::query()
            ->where('id', $id)
            ->get_one();
        \Cache::set('product_units.' . $id, $product_unit);
    }

    and it works perfectly.


    But, i wonder why I have to do that with the cache on the ORM : https://fuelphp.com/docs/packages/orm/intro.html

    I suppose if i don't want to use cache I have to call ->from_cache(false). But it's not the case.

    And i tried to use cache defined here : https://fuelphp.com/docs/classes/database/usage.html
    but it seems "->cached(3600, "foo.bar", false)" could be used only with DB::query.


    I search in config if i have a 'caching' => true, but no.
    I try to find a "\Orm\Query::caching(false);", but no.

    Maybe I don't understand something ?

    How to cache a query ? Maybe like this : ->cached(3600, "foo.bar", false) ?
  • You're mixing two concepts: the ORM object cache, and a data cache.

    The ORM by defaults caches all results (all model objects) in memory by primary key, so that if you fire two queries that have the same record in the result set, the same model object is returned (as opposed to a different object with the same contents). This means that if you change a property in one result, it is also changed in the other, in other words, the object data is consistent. Also, a find() by primary key will return the cached object if present instead of executing another query.

    The downside of this is two-fold: you actually may not want this behaviour, and if you process large amount of records, for example in some sort of batch operation, you may run out of memory. To deal with this, you can disable this behaviour on a per-query basis using from_cache(), or globally through the orm config file.

    Data caching is done by the Cache class, which sorts a data stucture in local cache storage where it can be retrieved later to offload (in this case) the database. The ORM has no built-in support for data caching, it indeed doesn't support the cached() method from the DB classes. I admit it might be a handy addition.

    At the moment, the only way to cache data from an ORM query is manually:

    $key = "some cache key"
    $cache = \Cache::forge($key);
    try
    {
        $result = $cache->get();
    }
    catch (\CacheNotFoundException $e)
    {
        $result = your-query-here...
        if ($result)
        {
            $cache->set($result, 3600);
        }
    }

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion