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

first try caching - 500 error

in services.php:

use Phalcon\Cache\Frontend\Data as FrontendData;
use Phalcon\Cache\Backend\Memcache as BackendMemcache;

// Set the models cache service
$di->set('modelsCache', function () {

    // Cache data for one day by default
    $frontCache = new FrontendData(
        array(
            "lifetime" => 86400
        )
    );

    // Memcached connection settings
    $cache = new BackendMemcache(
        $frontCache,
        array(
            "host" => "localhost",
            "port" => "11211"
        )
    );

    return $cache;
});

Query in IndexController:

        $pathways = GpcrDownstreamPathway::find(
                  array(
                        "cache" => array(
                                "key" => "my-cache"
                                )
                        )
                  );

That causes a 500 error. If I do this:

$pathways = GpcrDownstreamPathway::find();

It works fine. Any ideas?



17.5k
edited Aug '16

This works for me...

services.php

use Phalcon\Cache\Frontend\Data as FrontData;
use Phalcon\Cache\Backend\Memcache as BackMemCached;

$di->set('cache',function(){
    $frontCache = new FrontData([
        "lifetime" => 900
    ]);
    $cache = new BackMemCached(
        $frontCache,
        [
            "host" => "localhost",
            "port" => 11211,
            "persistent" => false
        ]
    );

    return $cache;
});

Inside my model to save:

$cache = $this->getDi()->getShared('cache');
$cache->save($key,$data,300); //300 is the time to save

Inside my model to retrieve:

$cache = $this->getDi()->getShared('cache');
$data = $cache->get($key);


40.7k

Where are those last two code blocks? Inside the model? Inside the save() and find() functions? You override them? None of the documentation includes those steps...



17.5k
edited Aug '16

The first is my services.php file. The secod is an example of how I save data to the cache. The last code blocks are how I access the data in cache.

I do all of the cache setting/getting outside of the database calls to find and save. For instance, find would look something like this:

    $key = 'userorders'.$user->id //pretend you already have a user object.
    if (!$cache->has($key)) {
        $data = Order::find(
            "conditions" => "user_id = :id:",
            "bind" => [
                "id" => $user->id
            ]
        );
        $cache->save($key,$data,300);
    }
    return $cache->get($key);

If it's possible to cache the response from the database automatically, I'm not sure how to do that.

Chceck the logs. Error 500 means some exception or something.



40.7k

I checked the logs /var/log/apache2/error_log and all I see in there is stuff about mod rewrite, nothing about phalcon. Is there another location that logs might be in?



17.5k

What is the error you're getting? mod rewrite must be enabled for Phalcon to work properly.

I still am not convinced that find() will work with cache. Is that somewhere in the docs? find() is an ORM function that requires a database connections AFAIK.



40.7k

Mod rewrite IS enabled:

$ sudo a2enmod rewrite
[sudo] password: 
Module rewrite already enabled

Phalcon 3.0 supports model results caching, which is what I am trying to implement:

https://docs.phalcon.io/en/latest/reference/cache.html

Ordinarily I would look in /var/log/apache2/error_log for stuff involving that line but nothing is there except things about rewrite. And like I mentioned a simple find() works and the page displays so the problem isn't rewrite.

edited Aug '16

just do:

try {

}
catch (Exception $e) {
echo $e->getMessage()
}

On your application or whatever which you are using or where you are doing this find with cache. For 100% you have some kind of error, like not running memcached or whatever or not installed php extension with memcache/memcached. There for 100% must be some log/exception, you can't just have error 500 because of nothing.



40.7k

here is the output from the logs when I try to load the page:

[Mon Aug 15 16:12:42.575828 2016] [rewrite:trace3] [pid 26861] mod_rewrite.c(468): [client 156.145.113.22:40485] 156.145.113.22 - - [gote.tatonettilab.org/sid#7f685d51ebd0][rid#7f685d4660a0/initial] [perdir /var/www/gote.tatonettilab.org/public_html/gote/] strip per-dir prefix: /var/www/gote.tatonettilab.org/public_html/gote/ -> 
[Mon Aug 15 16:12:42.575853 2016] [rewrite:trace3] [pid 26861] mod_rewrite.c(468): [client 156.145.113.22:40485] 156.145.113.22 - - [gote.tatonettilab.org/sid#7f685d51ebd0][rid#7f685d4660a0/initial] [perdir /var/www/gote.tatonettilab.org/public_html/gote/] applying pattern '^$' to uri ''
[Mon Aug 15 16:12:42.575860 2016] [rewrite:trace2] [pid 26861] mod_rewrite.c(468): [client 156.145.113.22:40485] 156.145.113.22 - - [gote.tatonettilab.org/sid#7f685d51ebd0][rid#7f685d4660a0/initial] [perdir /var/www/gote.tatonettilab.org/public_html/gote/] rewrite '' -> 'public/'
[Mon Aug 15 16:12:42.575864 2016] [rewrite:trace3] [pid 26861] mod_rewrite.c(468): [client 156.145.113.22:40485] 156.145.113.22 - - [gote.tatonettilab.org/sid#7f685d51ebd0][rid#7f685d4660a0/initial] [perdir /var/www/gote.tatonettilab.org/public_html/gote/] add per-dir prefix: public/ -> /var/www/gote.tatonettilab.org/public_html/gote/public/
[Mon Aug 15 16:12:42.575869 2016] [rewrite:trace2] [pid 26861] mod_rewrite.c(468): [client 156.145.113.22:40485] 156.145.113.22 - - [gote.tatonettilab.org/sid#7f685d51ebd0][rid#7f685d4660a0/initial] [perdir /var/www/gote.tatonettilab.org/public_html/gote/] strip document_root prefix: /var/www/gote.tatonettilab.org/public_html/gote/public/ -> /gote/public/
[Mon Aug 15 16:12:42.575885 2016] [rewrite:trace1] [pid 26861] mod_rewrite.c(468): [client 156.145.113.22:40485] 156.145.113.22 - - [gote.tatonettilab.org/sid#7f685d51ebd0][rid#7f685d4660a0/initial] [perdir /var/www/gote.tatonettilab.org/public_html/gote/] internal redirect with /gote/public/ [INTERNAL REDIRECT]
[Mon Aug 15 16:12:42.575913 2016] [rewrite:trace3] [pid 26861] mod_rewrite.c(468): [client 156.145.113.22:40485] 156.145.113.22 - - [gote.tatonettilab.org/sid#7f685d51ebd0][rid#7f685d4600a0/subreq] [perdir /var/www/gote.tatonettilab.org/public_html/gote/] strip per-dir prefix: /var/www/gote.tatonettilab.org/public_html/gote/index.php -> index.php
[Mon Aug 15 16:12:42.575918 2016] [rewrite:trace3] [pid 26861] mod_rewrite.c(468): [client 156.145.113.22:40485] 156.145.113.22 - - [gote.tatonettilab.org/sid#7f685d51ebd0][rid#7f685d4600a0/subreq] [perdir /var/www/gote.tatonettilab.org/public_html/gote/] applying pattern '^$' to uri 'index.php'
[Mon Aug 15 16:12:42.575921 2016] [rewrite:trace3] [pid 26861] mod_rewrite.c(468): [client 156.145.113.22:40485] 156.145.113.22 - - [gote.tatonettilab.org/sid#7f685d51ebd0][rid#7f685d4600a0/subreq] [perdir /var/www/gote.tatonettilab.org/public_html/gote/] strip per-dir prefix: /var/www/gote.tatonettilab.org/public_html/gote/index.php -> index.php
[Mon Aug 15 16:12:42.575923 2016] [rewrite:trace3] [pid 26861] mod_rewrite.c(468): [client 156.145.113.22:40485] 156.145.113.22 - - [gote.tatonettilab.org/sid#7f685d51ebd0][rid#7f685d4600a0/subreq] [perdir /var/www/gote.tatonettilab.org/public_html/gote/] applying pattern '(.*)' to uri 'index.php'
[Mon Aug 15 16:12:42.575927 2016] [rewrite:trace2] [pid 26861] mod_rewrite.c(468): [client 156.145.113.22:40485] 156.145.113.22 - - [gote.tatonettilab.org/sid#7f685d51ebd0][rid#7f685d4600a0/subreq] [perdir /var/www/gote.tatonettilab.org/public_html/gote/] rewrite 'index.php' -> 'public/index.php'
[Mon Aug 15 16:12:42.575931 2016] [rewrite:trace3] [pid 26861] mod_rewrite.c(468): [client 156.145.113.22:40485] 156.145.113.22 - - [gote.tatonettilab.org/sid#7f685d51ebd0][rid#7f685d4600a0/subreq] [perdir /var/www/gote.tatonettilab.org/public_html/gote/] add per-dir prefix: public/index.php -> /var/www/gote.tatonettilab.org/public_html/gote/public/index.php
[Mon Aug 15 16:12:42.575934 2016] [rewrite:trace2] [pid 26861] mod_rewrite.c(468): [client 156.145.113.22:40485] 156.145.113.22 - - [gote.tatonettilab.org/sid#7f685d51ebd0][rid#7f685d4600a0/subreq] [perdir /var/www/gote.tatonettilab.org/public_html/gote/] strip document_root prefix: /var/www/gote.tatonettilab.org/public_html/gote/public/index.php -> /gote/public/index.php
[Mon Aug 15 16:12:42.575938 2016] [rewrite:trace1] [pid 26861] mod_rewrite.c(468): [client 156.145.113.22:40485] 156.145.113.22 - - [gote.tatonettilab.org/sid#7f685d51ebd0][rid#7f685d4600a0/subreq] [perdir /var/www/gote.tatonettilab.org/public_html/gote/] internal redirect with /gote/public/index.php [INTERNAL REDIRECT]
[Mon Aug 15 16:12:42.575951 2016] [rewrite:trace3] [pid 26861] mod_rewrite.c(468): [client 156.145.113.22:40485] 156.145.113.22 - - [gote.tatonettilab.org/sid#7f685d51ebd0][rid#7f685d45e0a0/subreq] [perdir /var/www/gote.tatonettilab.org/public_html/gote/] strip per-dir prefix: /var/www/gote.tatonettilab.org/public_html/gote/index.html -> index.html
[Mon Aug 15 16:12:42.575954 2016] [rewrite:trace3] [pid 26861] mod_rewrite.c(468): [client 156.145.113.22:40485] 156.145.113.22 - - [gote.tatonettilab.org/sid#7f685d51ebd0][rid#7f685d45e0a0/subreq] [perdir /var/www/gote.tatonettilab.org/public_html/gote/] applying pattern '^$' to uri 'index.html'
[Mon Aug 15 16:12:42.575957 2016] [rewrite:trace3] [pid 26861] mod_rewrite.c(468): [client 156.145.113.22:40485] 156.145.113.22 - - [gote.tatonettilab.org/sid#7f685d51ebd0][rid#7f685d45e0a0/subreq] [perdir /var/www/gote.tatonettilab.org/public_html/gote/] strip per-dir prefix: /var/www/gote.tatonettilab.org/public_html/gote/index.html -> index.html
[Mon Aug 15 16:12:42.575964 2016] [rewrite:trace3] [pid 26861] mod_rewrite.c(468): [client 156.145.113.22:40485] 156.145.113.22 - - [gote.tatonettilab.org/sid#7f685d51ebd0][rid#7f685d45e0a0/subreq] [perdir /var/www/gote.tatonettilab.org/public_html/gote/] applying pattern '(.*)' to uri 'index.html'
[Mon Aug 15 16:12:42.575967 2016] [rewrite:trace2] [pid 26861] mod_rewrite.c(468): [client 156.145.113.22:40485] 156.145.113.22 - - [gote.tatonettilab.org/sid#7f685d51ebd0][rid#7f685d45e0a0/subreq] [perdir /var/www/gote.tatonettilab.org/public_html/gote/] rewrite 'index.html' -> 'public/index.html'
[Mon Aug 15 16:12:42.575970 2016] [rewrite:trace3] [pid 26861] mod_rewrite.c(468): [client 156.145.113.22:40485] 156.145.113.22 - - [gote.tatonettilab.org/sid#7f685d51ebd0][rid#7f685d45e0a0/subreq] [perdir /var/www/gote.tatonettilab.org/public_html/gote/] add per-dir prefix: public/index.html -> /var/www/gote.tatonettilab.org/public_html/gote/public/index.html
[Mon Aug 15 16:12:42.575974 2016] [rewrite:trace2] [pid 26861] mod_rewrite.c(468): [client 156.145.113.22:40485] 156.145.113.22 - - [gote.tatonettilab.org/sid#7f685d51ebd0][rid#7f685d45e0a0/subreq] [perdir /var/www/gote.tatonettilab.org/public_html/gote/] strip document_root prefix: /var/www/gote.tatonettilab.org/public_html/gote/public/index.html -> /gote/public/index.html
[Mon Aug 15 16:12:42.575977 2016] [rewrite:trace1] [pid 26861] mod_rewrite.c(468): [client 156.145.113.22:40485] 156.145.113.22 - - [gote.tatonettilab.org/sid#7f685d51ebd0][rid#7f685d45e0a0/subreq] [perdir /var/www/gote.tatonettilab.org/public_html/gote/] internal redirect with /gote/public/index.html [INTERNAL REDIRECT]
[Mon Aug 15 16:12:42.576037 2016] [rewrite:trace3] [pid 26861] mod_rewrite.c(468): [client 156.145.113.22:40485] 156.145.113.22 - - [gote.tatonettilab.org/sid#7f685d51ebd0][rid#7f685d4630e8/initial/redir#1] [perdir /var/www/gote.tatonettilab.org/public_html/gote/public/] strip per-dir prefix: /var/www/gote.tatonettilab.org/public_html/gote/public/index.html -> index.html
[Mon Aug 15 16:12:42.576043 2016] [rewrite:trace3] [pid 26861] mod_rewrite.c(468): [client 156.145.113.22:40485] 156.145.113.22 - - [gote.tatonettilab.org/sid#7f685d51ebd0][rid#7f685d4630e8/initial/redir#1] [perdir /var/www/gote.tatonettilab.org/public_html/gote/public/] applying pattern '^(.*)$' to uri 'index.html'
[Mon Aug 15 16:12:42.576049 2016] [rewrite:trace2] [pid 26861] mod_rewrite.c(468): [client 156.145.113.22:40485] 156.145.113.22 - - [gote.tatonettilab.org/sid#7f685d51ebd0][rid#7f685d4630e8/initial/redir#1] [perdir /var/www/gote.tatonettilab.org/public_html/gote/public/] rewrite 'index.html' -> 'index.php?_url=/index.html'
[Mon Aug 15 16:12:42.576053 2016] [rewrite:trace3] [pid 26861] mod_rewrite.c(468): [client 156.145.113.22:40485] 156.145.113.22 - - [gote.tatonettilab.org/sid#7f685d51ebd0][rid#7f685d4630e8/initial/redir#1] split uri=index.php?_url=/index.html -> uri=index.php, args=_url=/index.html
[Mon Aug 15 16:12:42.576056 2016] [rewrite:trace3] [pid 26861] mod_rewrite.c(468): [client 156.145.113.22:40485] 156.145.113.22 - - [gote.tatonettilab.org/sid#7f685d51ebd0][rid#7f685d4630e8/initial/redir#1] [perdir /var/www/gote.tatonettilab.org/public_html/gote/public/] add per-dir prefix: index.php -> /var/www/gote.tatonettilab.org/public_html/gote/public/index.php
[Mon Aug 15 16:12:42.576059 2016] [rewrite:trace2] [pid 26861] mod_rewrite.c(468): [client 156.145.113.22:40485] 156.145.113.22 - - [gote.tatonettilab.org/sid#7f685d51ebd0][rid#7f685d4630e8/initial/redir#1] [perdir /var/www/gote.tatonettilab.org/public_html/gote/public/] strip document_root prefix: /var/www/gote.tatonettilab.org/public_html/gote/public/index.php -> /gote/public/index.php
[Mon Aug 15 16:12:42.576062 2016] [rewrite:trace1] [pid 26861] mod_rewrite.c(468): [client 156.145.113.22:40485] 156.145.113.22 - - [gote.tatonettilab.org/sid#7f685d51ebd0][rid#7f685d4630e8/initial/redir#1] [perdir /var/www/gote.tatonettilab.org/public_html/gote/public/] internal redirect with /gote/public/index.php [INTERNAL REDIRECT]
[Mon Aug 15 16:12:42.576085 2016] [rewrite:trace3] [pid 26861] mod_rewrite.c(468): [client 156.145.113.22:40485] 156.145.113.22 - - [gote.tatonettilab.org/sid#7f685d51ebd0][rid#7f685d481370/initial/redir#2] [perdir /var/www/gote.tatonettilab.org/public_html/gote/public/] strip per-dir prefix: /var/www/gote.tatonettilab.org/public_html/gote/public/index.php -> index.php
[Mon Aug 15 16:12:42.576089 2016] [rewrite:trace3] [pid 26861] mod_rewrite.c(468): [client 156.145.113.22:40485] 156.145.113.22 - - [gote.tatonettilab.org/sid#7f685d51ebd0][rid#7f685d481370/initial/redir#2] [perdir /var/www/gote.tatonettilab.org/public_html/gote/public/] applying pattern '^(.*)$' to uri 'index.php'
[Mon Aug 15 16:12:42.576094 2016] [rewrite:trace1] [pid 26861] mod_rewrite.c(468): [client 156.145.113.22:40485] 156.145.113.22 - - [gote.tatonettilab.org/sid#7f685d51ebd0][rid#7f685d481370/initial/redir#2] [perdir /var/www/gote.tatonettilab.org/public_html/gote/public/] pass through /var/www/gote.tatonettilab.org/public_html/gote/public/index.php

I wrapped my code in a try block like you suggested and the 500 error is still happening and no output on the page from the echo in the catch block :(

edited Aug '16

Maybe you have something in dispatcher what is handling exceptions ? Some listener or something ? Post your whole application to github - otherwise we can't reproduce this or help. Or make script to reproduce issue.

But my possible guess and solution is this:

pecl install memcache

and restart your webserver



40.7k

Aha! Memcache appears to have fixed the 500 error. Thanks for that!

But I am still not getting results, and no errors are being thrown in the logs anywhere. Here is the code:

https://github.com/blm14/gote2

edited Aug '16

But what is happening now ?



40.7k

just no results from find() and still no error messages in /var/log/apache2/error_log except for mod rewrite

But maybe you have error messages in proper error file for vhost ? Check your apache config men.



17.5k
edited Aug '16

Not sure why you're overriding the find and findFirst methods in your model. Try changing the names of these methods in your model & see if it changes your results.



17.5k
edited Aug '16
    /**
     * Allows to query a set of records that match the specified conditions
     *
     * @param mixed $parameters
     * @return GpcrDownstreamPathway[]
     */
    public static function findInModel($parameters = null)
    {
        return GpcrDownstreamPathway::find($parameters);
    }
    /**
     * Allows to query the first record that match the specified conditions
     *
     * @param mixed $parameters
     * @return GpcrDownstreamPathway
     */
    public static function findFirstInModel($parameters = null)
    {
        return GpcrDownstreamPathway::findFirst($parameters);
    }
    public function indexAction()
    {
       //$pathways = GpcrDownstreamPathway::findInModel(array("parameters"=>"GPCR_symbol = 'ACKR3'","limit"=>"100"));   
        print("trying to pull");
    try{
        $pathways = GpcrDownstreamPathway::findInModel(
              array(
                "cache" => array(
                            "key" => "my-cache"
                            )
                   )
         );
                 print("success!");
        }catch(Phalcon\Exception $e){
                print("exception!");
        print $e->getMessage();
    }
    $this->view->pathways = $pathways;
    }


17.5k

Actually, you don't necessarily need your model in this case unless you're doing some post-processing. You could simplify to:

    $GDP = new GpcrDownstreamPathway();
    $pathways = $GDP->find([
        "cache" => [
            "key" => "my-cache"
        ]
    ]);

    if (!empty($pathways)) {
        return $pathways->toArray();
    }

    return false;

But get rid of the find and findFirst methods in your GpcrDownstreamPathway class.



40.7k

Not sure why you're overriding the find and findFirst methods in your model. Try changing the names of these methods in your model & see if it changes your results.

I used phalcon-devtools to generate that model. It must have done that, I can't speak to why!



40.7k

I commented out those functions in the model and used the following code in IndexController:

    $GDP = new GpcrDownstreamPathway();
    $pathways = $GDP->find([
        "cache" => [
            "key" => "my-cache"
        ]
    ]);

    if (!empty($pathways)) {
        return $pathways->toArray();
    }

    return false;

and now the page says:

Cannot connect to Memcached server
#0 [internal function]: Phalcon\Cache\Backend\Memcache->_connect()
#1 [internal function]: Phalcon\Cache\Backend\Memcache->get('my-cache', 3600)
#2 [internal function]: Phalcon\Mvc\Model\Query->execute()
#3 /var/www/gote.tatonettilab.org/public_html/gote/app/controllers/IndexController.php(17): Phalcon\Mvc\Model::find(Array)
#4 [internal function]: IndexController->indexAction()
#5 [internal function]: Phalcon\Dispatcher->callActionMethod(Object(IndexController), 'indexAction', Array)
#6 [internal function]: Phalcon\Dispatcher->_dispatch()
#7 [internal function]: Phalcon\Dispatcher->dispatch()
#8 /var/www/gote.tatonettilab.org/public_html/gote/public/index.php(29): Phalcon\Mvc\Application->handle()
#9 {main}

So it does look like some problem with memcache. But from phpinfo:

memcache

memcache support enabled Active persistent connections 0 Version 2.2.7 Revision $Revision: 327750 $

Directive Local Value Master Value memcache.allow_failover 1 1 memcache.chunk_size 8192 8192 memcache.default_port 11211 11211 memcache.default_timeout_ms 1000 1000 memcache.hash_function crc32 crc32 memcache.hash_strategy standard standard memcache.max_failover_attempts 20 20



40.7k

Hrm, memcached wasn't installed. I did sudo apt-get install memcached and that error above went away. The query is still not working with either versions of the code and I am still not seeing any errors in the logs from phalcon :(



40.7k

OK, good news. I set the php error log location in /etc/php5/apache2/php.ini and now I see phalcon php errors!

First I saw this:

Allowed memory size of 134217728 bytes exhausted (tried to allocate 32 bytes) in /var/www/gote.tatonettilab.org/public_html/gote/app/controllers/IndexController.php on line 38

So I increased the memory size from 128m to 512m in /etc/php5/apache2/php.ini and restarted apache.

Now I am seeing this:

Failed storing data in memcached
#0 [internal function]: Phalcon\Cache\Backend\Memcache->save('my-cache', Object(Phalcon\Mvc\Model\Resultset\Simple), 3600)
#1 [internal function]: Phalcon\Mvc\Model\Query->execute()
#2 /var/www/gote.tatonettilab.org/public_html/gote/app/controllers/IndexController.php(22): Phalcon\Mvc\Model::find(Array)
#3 [internal function]: IndexController->indexAction()
#4 [internal function]: Phalcon\Dispatcher->callActionMethod(Object(IndexController), 'indexAction', Array)
#5 [internal function]: Phalcon\Dispatcher->_dispatch()
#6 [internal function]: Phalcon\Dispatcher->dispatch()
#7 /var/www/gote.tatonettilab.org/public_html/gote/public/index.php(29): Phalcon\Mvc\Application->handle()
#8 {main}
edited Aug '16

What a hell, did you really have so big resultset or what ? ;o What phalcon version and php version you are using ?



40.7k
mysql> SELECT (data_length+index_length)/power(1024,2) tablesize_mb FROM information_schema.tables WHERE table_schema='gote' and table_name='GPCR_downstream_pathway';
+--------------+
| tablesize_mb |
+--------------+
|     9.515625 |
+--------------+
1 row in set (0.00 sec)

mysql> select count(1) from gote.GPCR_downstream_pathway;
+----------+
| count(1) |
+----------+
|    65534 |
+----------+
1 row in set (0.01 sec)

Doesn't seem too terrible...

$ php -v
PHP 5.5.9-1ubuntu4.19 (cli) (built: Jul 28 2016 19:31:33) 
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2014 Zend Technologies
    with Zend OPcache v7.0.3, Copyright (c) 1999-2014, by Zend Technologies

from phpinfo():

Web framework delivered as a C-extension for PHP
phalcon => enabled
Author => Phalcon Team and contributors
Version => 3.0.0
Build Date => Aug 11 2016 15:43:28
Powered by Zephir => Version 0.9.3a-dev-e716dbe641


17.5k

Phalcon is also compiled and also stored in memory... I've seen that error before caching large sets of json (~30M).

If I were you, I would start simple. Try caching a string, then try caching the response from a query, then try caching the query, itself like you're trying to do now. It would seem to me there's something wrong with your memcache setup.



40.7k

Do you mean try caching a string using phalcon? Or directly using memcached? I don't know how to cache a string using phalcon...



17.5k

My first comment outlines how to cache using Phalcon. I'm not 100% if this method has changed in Phalcon 3, but it worked in 2.



40.7k
edited Aug '16

OK, so here is what I have now:

services.php

// Set the models cache service
$di->set('modelsCache', function () {

    // Cache data for one day by default
    $frontCache = new FrontendData(
        array(
            "lifetime" => 86400
        )
    );

    // Memcached connection settings
    $cache = new BackendMemcache(
        $frontCache,
        array(
            "host" => "localhost",
            "port" => "11211"
        )
    );

    return $cache;
});

This is the entire content of IndexController's indexAction():

        print("trying to save");

        $cache = $this->getDi()->getShared('modelsCache');
        $cache->save($key,$data,300); //300 is the time to save        

        print('saved!');
        print('trying to retrieve');
        $cache = $this->getDi()->getShared('modelsCache');
        $data = $cache->get($key);

        print('done!');

and here is the output:

trying to saveCache must be started first
#0 /var/www/gote.tatonettilab.org/public_html/gote/app/controllers/IndexController.php(15): Phalcon\Cache\Backend\Memcache->save(NULL, NULL, 300)
#1 [internal function]: IndexController->indexAction()
#2 [internal function]: Phalcon\Dispatcher->callActionMethod(Object(IndexController), 'indexAction', Array)
#3 [internal function]: Phalcon\Dispatcher->_dispatch()
#4 [internal function]: Phalcon\Dispatcher->dispatch()
#5 /var/www/gote.tatonettilab.org/public_html/gote/public/index.php(29): Phalcon\Mvc\Application->handle()
#6 {main}

What is "cache must be started first" ?



17.5k

The values of $key and $data are null. The $key should be "my-cache" per your above comments. The $data is the data you want to save. At this point, I would say $data = "Testing cache"; or something like that.



40.7k

OK, that worked. Now when I try to go back to my original code it is throwing yet a NEW exception:

Cache didn't return a valid resultset
#0 [internal function]: Phalcon\Mvc\Model\Query->execute()
#1 /var/www/gote.tatonettilab.org/public_html/gote/app/controllers/IndexController.php(56): Phalcon\Mvc\Model::find(Array)
#2 [internal function]: IndexController->indexAction()
#3 [internal function]: Phalcon\Dispatcher->callActionMethod(Object(IndexController), 'indexAction', Array)
#4 [internal function]: Phalcon\Dispatcher->_dispatch()
#5 [internal function]: Phalcon\Dispatcher->dispatch()
#6 /var/www/gote.tatonettilab.org/public_html/gote/public/index.php(29): Phalcon\Mvc\Application->handle()
#7 {main}

code is just this:

            $pathways = GpcrDownstreamPathway::find(
                array(
                    "cache" => array(
                        "key" => "my-cache"
                    )
                )
            );
            $this->view->pathways = $pathways;
            echo("success!");

What you are trying to do ? Looks like you are trying to save content of view to cache somewhere and then trying to use this key in find method which obviously won't work.



40.7k

Ahhhh, so finally we find the real issue. User error! :(

I am trying to first a) query model, b) cache results, c) return results to view.

Probably what I SHOULD be doing is: check to see if results are in cache and if so, return them, otherwise run query, stick results in cache, and then return results. No clue how to do that though. Sorry for being such a n00b!



17.5k

What you're describing is what I've outlined in my second response.



40.7k

I really feel like I am missing something obvious here. Saving just strings into the cache worked. Here is my current code block from the controller:

        $cachekey = "my-cache";
        $cache = $this->di->getShared("modelsCache");

        $pathways = $cache->get($cachekey);

        echo("pathways = ".$pathways.'<BR>');

        if($pathways == null){
            $pathways = GpcrDownstreamPathway::find();
        }

        $cache->save($cachekey,$pathways);

        $this->view->pathways = $pathways;

and I am still getting this:

pathways = 
Failed storing data in memcached
#0 /var/www/gote.tatonettilab.org/public_html/gote/app/controllers/IndexController.php(20): Phalcon\Cache\Backend\Memcache->save('my-cache', Object(Phalcon\Mvc\Model\Resultset\Simple))
#1 [internal function]: IndexController->indexAction()
#2 [internal function]: Phalcon\Dispatcher->callActionMethod(Object(IndexController), 'indexAction', Array)
#3 [internal function]: Phalcon\Dispatcher->_dispatch()
#4 [internal function]: Phalcon\Dispatcher->dispatch()
#5 /var/www/gote.tatonettilab.org/public_html/gote/public/index.php(29): Phalcon\Mvc\Application->handle()
#6 {main}

As i wrote earlier - i guess that this data is just too much for saving for some reason. Can you for instance check if you can save some smaller portion of data ? :D



17.5k
edited Aug '16
        $cachekey = "my-cache";
        $cache = $this->di->getShared("modelsCache");

        $pathways = $cache->get($cachekey);

        echo("pathways = ".$pathways.'<BR>');

        if(empy($pathways)){
            $pathways = GpcrDownstreamPathway::find();
        }

        $cache->save($cachekey,$pathways->toArray());

        $this->view->pathways = $pathways;

The problem is you're caching the collection object and those can be huge. If you want to simply cache the data, ->toArray() will give you the data plus any relations you've defined.



40.7k

Yup! Adding array("limit"=>"100") as a parameter inside find() and it works. So the question is how can I cache the entire table? Use disk instead of memcached? Or will that be just as slow? :(



40.7k

Hrm, memcached.conf has a 64 meg limit. Let's try increasing that, shall we!

edited Aug '16

But it's kind of problem, i don't know why this is so much space. Maybe try to cache just array(toArray method), and get this array to display your self(instead of using cache keyword just save and get it yourself in cache) intead of full objects ? Also did you really need all columns in this resultset ? Are you displaying them all in view ?

Perhaps you can even compress this further using json_encode+gzcompress maybe ?