We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

caching model metadata simple example

Hi There,

I'm new here, i'm facing problem with default call to "all_tab_columns" Oracle DB process consuming 90% CPU at application peak time, need simple and sample code snippet to implement cache using with memcache or any other cache mechanism

Thanks! in advance

edited Jul '19

Try this at Your startup / bootstrap:

new \Phalcon\Di\FactoryDefault();
$di = \Phalcon\di::getDefault();

$di->set('modelsMetadata', function () {

$metaData = new Phalcon\Mvc\Model\Metadata\Libmemcached(
    [
        "servers" => [
            [
                "host"   => "localhost",
                "port"   => 11211,
                "weight" => 1,
            ],
        ],
        "client" => [
            \Memcached::OPT_HASH       => \Memcached::HASH_MD5,
            \Memcached::OPT_PREFIX_KEY => "prefix.",
        ],
        "lifetime" => 3600,
        "prefix"   => "my_",
    ]
);

return $metadata;
});

..and read: https://docs.phalcon.io/3.4/en/api/phalcon_mvc_model_metadata

edited Jul '19

Thanks!

Do i need to do anything else other than adding above code to service/startup/bootstrap? i mean should i add anything in my model class?

Thanks!

Do i need to do anything else other than adding above code to service/startup/bootstrap? i mean should i add anything in my model class?

No, it should do the job

Thanks! #Tomasz Zadora let me try this.. by the way version wont be a problem for this i believe, because i'm using "1.3"

Version may be a problem, switching to ver. 3.4 is recommended

Hi Zadora,

How can we implemet model meta data caching with APC, i have tried this from the docs but no luck! can you assist me on this

$di['modelsMetadata'] = function() { $metaData = new \Phalcon\Mvc\Model\MetaData\Apc(array( "lifetime" => 3600, "prefix" => "cache-my-model" )); return $metaData; };

With Libmemcached its not working because i have 1.3 version.

Thanks! in advance

Sorry I have no idea for simple solution. You probably need to create own MetaData caching class that implements interfaces \Phalcon\Mvc\Model\MetaDataInterface and \Phalcon\Di\InjectionAwareInterface for example by extending Abstract class Phalcon\Mvc\Model\MetaData

https://docs.phalcon.io/3.4/en/api/phalcon_mvc_model_metadata