colinmollenhour / Cm_RedisSession

Redis-based session handler for Magento with optimistic locking
208 stars 121 forks source link

max_lifetime not being enforced for some keys #143

Closed wssweb closed 6 years ago

wssweb commented 6 years ago

When I check the keys in the session db there are some that are getting a ttl of 2592000 despite having set a max_lifetime of 43200 in local.xml. Most of the keys are set correctly with the 43200 life, and the bot keys are set to 300 as per the bot_lifetime. Over a few days these really start to add up.

The only other place I can find a reference to 2592000 is in app/code/community/CM/RedisSession/Model/Session.php:

    /**
     * Public for testing/import purposes only.
     *
     * @param $id
     * @param $data
     * @param $lifetime
     * @throws Exception
     */
    public function _writeRawSession($id, $data, $lifetime)
    {
        if ( ! $this->_useRedis) {
            throw new Exception('Not connected to redis!');
        }

        $sessionId = 'sess_' . $id;
        $this->_redis->pipeline()
            ->select($this->_dbNum)
            ->hMSet($sessionId, array(
                'data' => $this->_encodeData($data),
                'lock' => 0, // 0 so that next lock attempt will get 1
            ))
            ->hIncrBy($sessionId, 'writes', 1) // For informational purposes only
            ->expire($sessionId, min($lifetime, 2592000))
            ->exec();
    }

Is this supposed to bypass the max_lifetime set in the xml?

colinmollenhour commented 6 years ago

Strange.. Perhaps try removing the min() and see what lifetime is set instead.

wssweb commented 6 years ago

with:

    public function _writeRawSession($id, $data, $lifetime)
    {
        if ( ! $this->_useRedis) {
            throw new Exception('Not connected to redis!');
        }

        $sessionId = 'sess_' . $id;
        $this->_redis->pipeline()
            ->select($this->_dbNum)
            ->hMSet($sessionId, array(
                'data' => $this->_encodeData($data),
                'lock' => 0, // 0 so that next lock attempt will get 1
            ))
            ->hIncrBy($sessionId, 'writes', 1) // For informational purposes only
            ->expire($sessionId, $lifetime)
            ->exec();
    }

It sets a lifetime of just a hair over 100 years... (3155692593 seconds is the highest ttl I find)

colinmollenhour commented 6 years ago

I think you are not using the version from github but rather the bundled version which is outdated.

wssweb commented 6 years ago

I updated to to latest version of from github. I will let it run over night to see if any long ttl's show up.

I did notice that after updating it however that my system log is getting spammed with:

2018-03-29T17:58:48+00:00 ERR (3): Notice: Undefined offset: 2  in /var/www/html/lib/Cm/Cache/Backend/Redis.php on line 616
2018-03-29T17:58:48+00:00 ERR (3): Notice: Undefined offset: 1  in /var/www/html/lib/Cm/Cache/Backend/Redis.php on line 616
2018-03-29T17:58:48+00:00 ERR (3): Notice: Undefined offset: 0  in /var/www/html/lib/Cm/Cache/Backend/Redis.php on line 616

edit: nm, the undefined offset cleared up once I updated Cm_Cache_Backend_Redis as well

wssweb commented 6 years ago

It looks like this is fixed using the newest version.

I have noticed some slow down when clicking through pages on the site however (it appears to be hitting the break_after_frontend time rather easily). Will need to do more testing to see if I can narrow down whats going on there.

Thanks for the help Colin