New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apc backend can't store data via cli mode #3269

Closed
TrogWarZ opened this Issue Jan 22, 2015 · 9 comments

Comments

Projects
None yet
4 participants
@TrogWarZ

TrogWarZ commented Jan 22, 2015

\Phalcon\Cache\Backend\Apc don't save and don't get data if used from CLI task.
If used via server – Apc works good.
Other adapters (e.g., \Phalcon\Cache\Backend\Memcache) are okay too.

Searched in issues and forum but nothing was found.
Is this Phalcon bug? Or i'm doing something wrong?
And how can i fix/workaround this?

Thank you!

System info

# uname -a
Linux api-dev 2.6.32.26 #1 SMP Thu Dec 9 15:33:24 UTC 2010 x86_64 x86_64 x86_64 GNU/Linux
# php -v
PHP 5.4.36-1+deb.sury.org~precise+2 (cli) (built: Dec 21 2014 20:28:53)
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2014 Zend Technologies
    with Xdebug v2.2.3, Copyright (c) 2002-2013, by Derick Rethans
# php -r "echo \Phalcon\Version::get();"
1.3.4

$ uname -a
Darwin mac-boo.local 14.0.0 Darwin Kernel Version 14.0.0: Fri Sep 19 00:26:44 PDT 2014; root:xnu-2782.1.97~2/RELEASE_X86_64 x86_64
$ php -v
PHP 5.4.36 (cli) (built: Jan 17 2015 10:40:41)
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2014 Zend Technologies
    with Xdebug v2.2.6, Copyright (c) 2002-2014, by Derick Rethans
    with Suhosin v0.9.36, Copyright (c) 2007-2014, by SektionEins GmbH
$ php -r "echo \Phalcon\Version::get();"
1.3.4

In a nutshell, you can test it like that:

index.php

$di = new \Phalcon\DI\FactoryDefault\CLI();
$di->setShared('db', function () {
    return new \Phalcon\Db\Adapter\Pdo\Mysql(array(/* .. some config .. */));
});
$di->setShared('cache', function () {
    $frontend = new \Phalcon\Cache\Frontend\Data(array(
        'lifetime' => 60 * 15,
    ));

    // See cli task: tests are using only one backend at a time (Apc OR Memcache) to get different results
    return new \Phalcon\Cache\Multiple(array(
        new \Phalcon\Cache\Backend\Apc($frontend, array(
            'prefix' => 'api-apc',
        )),
        new \Phalcon\Cache\Backend\Memcache($frontend, array(
            'prefix' => 'api-memcache',
            'host'   => 'localhost',
            'port'   => 11211,
        )),
    ));
});

$app = new \Phalcon\Mvc\Micro($di);
$app->notFound(function () { die("Route not found."); });
$app->handle();

CLI task class

/**
 * Class MainTask
 *
 * @property \Phalcon\DI\FactoryDefault\CLI $di
 * @property \Phalcon\Cache\Multiple        $cache
 */
class MainTask extends \Phalcon\CLI\Task
{
    protected $di;
    protected $cache;

    public function mainAction() { }

    public function initialize()
    {
        $this->di    = $this->getDI();
        $this->cache = $this->di->getShared('cache');
    }

    public function testAction()
    {
        $id      = 42;
        $key     = "Users_$id";
        $user    = \Api\Models\Users::findFirst($id);
        $userArr = $user->toArray();
        $this->cache->save($key, $userArr);
        $userCached = $this->cache->get($key);

        // If APC is used:
        var_export($id);         echo "\n"; // [ OK ]: 42
        var_export($key);        echo "\n"; // [ OK ]: "Users_42"
        var_export($userArr);    echo "\n"; // [ OK ]: array(/* ... */)
        var_export($userCached); echo "\n"; // [FAIL]: NULL

        // If Memcached is used:
        var_export($id);         echo "\n"; // [ OK ]: 42
        var_export($key);        echo "\n"; // [ OK ]: "Users_42"
        var_export($userArr);    echo "\n"; // [ OK ]: array(/* ... */)
        var_export($userCached); echo "\n"; // [ OK ]: array(/* ... */)
    }
}

ps: Forum post: http://forum.phalconphp.com/discussion/4529/access-to-cache-in-cli-task-not-working-with-apc

@ovr

This comment has been minimized.

Show comment
Hide comment
Contributor

ovr commented Jan 22, 2015

@TrogWarZ

This comment has been minimized.

Show comment
Hide comment
@TrogWarZ

TrogWarZ Jan 22, 2015

Yes. Here some configs. Are they okay?

# php -m | grep apc
apc
# cat /etc/php5/conf.d/20-apc.ini
extension=apc.so
apc.enabled = 1
apc.enable_cli = 1
apc.shm_size = 256M

$ php -m | grep apc
apc
$ cat /usr/local/etc/php/5.4/conf.d/ext-apc.ini
[apc]
extension="/usr/local/Cellar/php54-apc/3.1.13/apc.so"
apc.enabled=1
apc.shm_segments=1
apc.shm_size=64M
apc.ttl=7200
apc.user_ttl=7200
apc.num_files_hint=1024
apc.mmap_file_mask=/tmp/apc.XXXXXX
apc.enable_cli=1

TrogWarZ commented Jan 22, 2015

Yes. Here some configs. Are they okay?

# php -m | grep apc
apc
# cat /etc/php5/conf.d/20-apc.ini
extension=apc.so
apc.enabled = 1
apc.enable_cli = 1
apc.shm_size = 256M

$ php -m | grep apc
apc
$ cat /usr/local/etc/php/5.4/conf.d/ext-apc.ini
[apc]
extension="/usr/local/Cellar/php54-apc/3.1.13/apc.so"
apc.enabled=1
apc.shm_segments=1
apc.shm_size=64M
apc.ttl=7200
apc.user_ttl=7200
apc.num_files_hint=1024
apc.mmap_file_mask=/tmp/apc.XXXXXX
apc.enable_cli=1
@sergeyklay

This comment has been minimized.

Show comment
Hide comment
@sergeyklay

sergeyklay Jan 22, 2015

Member

Hmm.. can you provide output of command: ls -l /etc/php5/ please?

Member

sergeyklay commented Jan 22, 2015

Hmm.. can you provide output of command: ls -l /etc/php5/ please?

@tsnyder91

This comment has been minimized.

Show comment
Hide comment
@tsnyder91

tsnyder91 Jan 22, 2015

APC CLI enabled only lets you store things in APC for that run of the script. So when you execute your CLI script the APC cache is created and can be used during your script, but once your cli script ends the APC cache is destroyed and closed.

MemcacheD is a seperate in memory storage system not tied to PHP's runtime environment, so it will live between CLI script executions.

Does that make sense?

tsnyder91 commented Jan 22, 2015

APC CLI enabled only lets you store things in APC for that run of the script. So when you execute your CLI script the APC cache is created and can be used during your script, but once your cli script ends the APC cache is destroyed and closed.

MemcacheD is a seperate in memory storage system not tied to PHP's runtime environment, so it will live between CLI script executions.

Does that make sense?

@TrogWarZ

This comment has been minimized.

Show comment
Hide comment
@TrogWarZ

TrogWarZ Jan 22, 2015

@sergeyklay sure:

# ls -l /etc/php5/
total 28K
drwxr-xr-x  7 root 4.0K Dec 25 21:20 ./
drwxr-xr-x 97 root 4.0K Jan 10 08:47 ../
drwxr-xr-x  2 root 4.0K Dec 25 21:20 apache2filter/
drwxr-xr-x  2 root 4.0K Dec 25 21:20 cli/
drwxr-xr-x  2 root 4.0K Dec 25 21:20 conf.d/
drwxr-xr-x  3 root 4.0K Dec 25 21:20 fpm/
drwxr-xr-x  2 root 4.0K Dec 25 21:20 mods-available/

$ ls -l /usr/local/etc/php/5.4/
total 240
drwxr-xr-x   7 trogwar  admin   238B Jan 17 10:41 ./
drwxr-xr-x   3 trogwar  admin   102B Oct 19 15:26 ../
drwxr-xr-x  16 trogwar  admin   544B Jan 17 10:56 conf.d/
-rw-r--r--   1 trogwar  admin   1.4K Jan 17 10:41 pear.conf
-rw-r--r--   1 trogwar  staff    22K Jan 14 18:31 php-fpm.conf
-rw-r--r--   1 trogwar  admin    22K Jan 17 10:40 php-fpm.conf.default
-rw-r--r--   1 trogwar  wheel    65K Jan 16 19:51 php.ini

TrogWarZ commented Jan 22, 2015

@sergeyklay sure:

# ls -l /etc/php5/
total 28K
drwxr-xr-x  7 root 4.0K Dec 25 21:20 ./
drwxr-xr-x 97 root 4.0K Jan 10 08:47 ../
drwxr-xr-x  2 root 4.0K Dec 25 21:20 apache2filter/
drwxr-xr-x  2 root 4.0K Dec 25 21:20 cli/
drwxr-xr-x  2 root 4.0K Dec 25 21:20 conf.d/
drwxr-xr-x  3 root 4.0K Dec 25 21:20 fpm/
drwxr-xr-x  2 root 4.0K Dec 25 21:20 mods-available/

$ ls -l /usr/local/etc/php/5.4/
total 240
drwxr-xr-x   7 trogwar  admin   238B Jan 17 10:41 ./
drwxr-xr-x   3 trogwar  admin   102B Oct 19 15:26 ../
drwxr-xr-x  16 trogwar  admin   544B Jan 17 10:56 conf.d/
-rw-r--r--   1 trogwar  admin   1.4K Jan 17 10:41 pear.conf
-rw-r--r--   1 trogwar  staff    22K Jan 14 18:31 php-fpm.conf
-rw-r--r--   1 trogwar  admin    22K Jan 17 10:40 php-fpm.conf.default
-rw-r--r--   1 trogwar  wheel    65K Jan 16 19:51 php.ini
@TrogWarZ

This comment has been minimized.

Show comment
Hide comment
@TrogWarZ

TrogWarZ Jan 22, 2015

@tsnyder91 this will be offtop-comment, mostly.

Yes, i know that if i reboot php-fpm that contents of APC data cache will be cleared. I also know that APC runs only on host, where code is.
In production, i have 32 Gb of RAM + script that "heats cache" + secondary memcached service on three other servers with 250 Gb total space.
But i'm still using APC as a front cache because it is much faster than memcached on my load profile and application-specific usecases.

With Phalcon+php-fpm i'm using them both: if APC fails then memcached can get data slower than APC, but faster than MySQL for just one request, cool! And APC greatly stores custom data between requests. But APC+CLI does not store data and.. i'm confusing – why? APC can't store data from php-cli by design? Or i'm configuring something wrong? Or this is bug?

TrogWarZ commented Jan 22, 2015

@tsnyder91 this will be offtop-comment, mostly.

Yes, i know that if i reboot php-fpm that contents of APC data cache will be cleared. I also know that APC runs only on host, where code is.
In production, i have 32 Gb of RAM + script that "heats cache" + secondary memcached service on three other servers with 250 Gb total space.
But i'm still using APC as a front cache because it is much faster than memcached on my load profile and application-specific usecases.

With Phalcon+php-fpm i'm using them both: if APC fails then memcached can get data slower than APC, but faster than MySQL for just one request, cool! And APC greatly stores custom data between requests. But APC+CLI does not store data and.. i'm confusing – why? APC can't store data from php-cli by design? Or i'm configuring something wrong? Or this is bug?

@sergeyklay

This comment has been minimized.

Show comment
Hide comment
@tsnyder91

This comment has been minimized.

Show comment
Hide comment
@tsnyder91

tsnyder91 Jan 22, 2015

@TrogWarZ APC's memory space is tied to the PHP runtime process, so by design php cli with always create a new apc space for the duration of the script and then destroy it after the script is over and the php cli runtime environment stops. So it can store data but initially it will always be empty.

Also, and this might be what you are having issues with, PHP CLI APC uses a separate memory space than the APC memory space that php-fpm uses. So you cannot run a PHP CLI script and access the APC data that php-fpm has and vice versa because they are executed in two different php runtime environments.

Unless you dump it out to a file like @sergeyklay's link shows, but that'd easily get out of sync and overly complicated in my opinion. I would personally use memcached only if you need a CLI script to be able to access data that a webapp is updating.

I think apc cli is really just for testing/debugging. Like making a quick script to test out how code works with an empty apc cache, populate and retrieve information tests, etc.

tsnyder91 commented Jan 22, 2015

@TrogWarZ APC's memory space is tied to the PHP runtime process, so by design php cli with always create a new apc space for the duration of the script and then destroy it after the script is over and the php cli runtime environment stops. So it can store data but initially it will always be empty.

Also, and this might be what you are having issues with, PHP CLI APC uses a separate memory space than the APC memory space that php-fpm uses. So you cannot run a PHP CLI script and access the APC data that php-fpm has and vice versa because they are executed in two different php runtime environments.

Unless you dump it out to a file like @sergeyklay's link shows, but that'd easily get out of sync and overly complicated in my opinion. I would personally use memcached only if you need a CLI script to be able to access data that a webapp is updating.

I think apc cli is really just for testing/debugging. Like making a quick script to test out how code works with an empty apc cache, populate and retrieve information tests, etc.

@TrogWarZ

This comment has been minimized.

Show comment
Hide comment
@TrogWarZ

TrogWarZ Jan 22, 2015

Oh.. i didn't know about problems with separating spaces between fpm/cli and dumpfiles. Thank you, @sergeyklay for useful SO post and thank you, @tsnyder91 for info about spaces.
Then, i think, issue can be closed as "doesn't make sense" anymore.

TrogWarZ commented Jan 22, 2015

Oh.. i didn't know about problems with separating spaces between fpm/cli and dumpfiles. Thank you, @sergeyklay for useful SO post and thank you, @tsnyder91 for info about spaces.
Then, i think, issue can be closed as "doesn't make sense" anymore.

@TrogWarZ TrogWarZ closed this Jan 22, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment