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

How can i remove '_PHCR' form the redis

How can i remove '_PHCR' form the redis I work the phalcon with the go and share the redis why should I add the '_PHCR' before the key



85.5k

i might be wrong, but you can't. But I do not understand why that key bothers you ?



77.7k
Accepted
answer

The _PHCR prefix is fixed in the source:

https://github.com/phalcon/cphalcon/blob/f0732e9c2ac4c234afa30fcd4dff1b94d24fe24a/phalcon/cache/backend/redis.zep#L31

You could create your own redis adapter based on that file.

I'm sure that code must be clear, it should tell me what it show me but not hide something for example: I write:

$app->getDI()['cache']->save('app_name', 'google');

somebody(golang) think the String 'google' maybe soted at 'app_name' so:

redisConn = redisPool.Get()
defer redisConn.Close()
appName, err := redisConn.Do('GET', 'app_name')
if err != nil {
    return "", err
}
return appName, nil

It's obvious,it return a error

i might be wrong, but you can't. But I do not understand why that key bothers you ?



85.5k
edited Jun '18

but this key name will always be the same. You can do that in your go app, for example

func (p *redisPool) getSharedKey( name string) (string, error) {
    redisConn := p.Get()
    defer redisConn.Close()
    const phalconKey = "_PHCR"
    v, err := redisConn.Do('GET', name + phalconKey)
    if err != nil {
        return "", err
    }
    return v, nil
}

the example just show the "_PHCR" is not friendly to multi-language development

but this key name will always be the same. You can do that in your go app, for example

func (p *redisPool) getSharedKey( name string) (string, error) {
  redisConn := p.Get()
  defer redisConn.Close()
  const phalconKey = "_PHCR"
  v, err := redisConn.Do('GET', name + phalconKey)
  if err != nil {
      return "", err
  }
  return v, nil
}


85.5k

sharing strings between applications using redis is not "multi-language development" . You need RPC for that

edited Dec '19

override the redis adapter

<?php

namespace App\Library\Cache\Backend;

use Phalcon\Cache\Exception;

class Redis extends \Phalcon\Cache\Backend\Redis
{

    /**
     * @var \Redis
     */
    protected $_redis;

    /**
     * {@inheritdoc}
     *
     * @param  string $keyName
     * @param  integer $lifetime
     * @return mixed|null
     */
    public function get($keyName, $lifetime = null)
    {
        $redis = $this->getRedis();

        /**
         * @var \Phalcon\Cache\FrontendInterface $frontend
         */
        $frontend = $this->_frontend;

        $lastKey = $this->getKeyName($keyName);

        $this->_lastKey = $lastKey;

        $content = $redis->get($lastKey);

        if ($content === false) {
            return null;
        }

        if (is_numeric($content)) {
            return $content;
        }

        return $frontend->afterRetrieve($content);
    }

    /**
     * {@inheritdoc}
     *
     * @param  string $keyName
     * @param  string $content
     * @param  int $lifetime
     * @param  bool $stopBuffer
     * @return bool
     *
     * @throws Exception
     */
    public function save($keyName = null, $content = null, $lifetime = null, $stopBuffer = true)
    {
        if ($keyName === null) {
            $lastKey = $this->_lastKey;
        } else {
            $lastKey = $this->getKeyName($keyName);
            $this->_lastKey = $lastKey;
        }

        if (!$lastKey) {
            throw new Exception('The cache must be started first');
        }

        $redis = $this->getRedis();

        /**
         * @var \Phalcon\Cache\FrontendInterface $frontend
         */
        $frontend = $this->_frontend;

        if ($content === null) {
            $cachedContent = $frontend->getContent();
        } else {
            $cachedContent = $content;
        }

        /**
         * Prepare the content in the frontend
         */
        if (!is_numeric($cachedContent)) {
            $preparedContent = $frontend->beforeStore($cachedContent);
        } else {
            $preparedContent = $cachedContent;
        }

        if ($lifetime === null) {
            $tmp = $this->_lastLifetime;
            $ttl = $tmp ? $tmp : $frontend->getLifetime();
        } else {
            $ttl = $lifetime;
        }

        $success = $redis->set($lastKey, $preparedContent);

        if (!$success) {
            throw new Exception('Failed storing the data in redis');
        }

        if ($ttl > 0) {
            $redis->setTimeout($lastKey, $ttl);
        }

        $isBuffering = $frontend->isBuffering();

        if ($stopBuffer === true) {
            $frontend->stop();
        }

        if ($isBuffering === true) {
            echo $cachedContent;
        }

        $this->_started = false;

        return $success;
    }

    /**
     * {@inheritdoc}
     *
     * @param  string $keyName
     * @return bool
     */
    public function delete($keyName)
    {
        $redis = $this->getRedis();

        $lastKey = $this->getKeyName($keyName);

        return (bool)$redis->delete($lastKey);
    }

    /**
     * {@inheritdoc}
     *
     * @param  string $prefix
     * @return array
     */
    public function queryKeys($prefix = null)
    {
        $redis = $this->getRedis();

        $pattern = "{$this->_prefix}" . ($prefix ? $prefix : '') . '*';

        return $redis->keys($pattern);
    }

    /**
     * {@inheritdoc}
     *
     * @param  string $keyName
     * @param  string $lifetime
     * @return bool
     */
    public function exists($keyName = null, $lifetime = null)
    {
        $redis = $this->getRedis();

        if ($keyName === null) {
            $lastKey = $this->_lastKey;
        } else {
            $lastKey = $this->getKeyName($keyName);
        }

        return (bool)$redis->exists($lastKey);
    }

    /**
     * {@inheritdoc}
     *
     * @param string $keyName
     * @param int $value
     * @return int
     */
    public function increment($keyName = null, $value = 1)
    {
        $redis = $this->getRedis();

        if ($keyName === null) {
            $lastKey = $this->_lastKey;
        } else {
            $lastKey = $this->getKeyName($keyName);
        }

        return $redis->incrBy($lastKey, $value);
    }

    /**
     * {@inheritdoc}
     *
     * @param string $keyName
     * @param int $value
     * @return int
     */
    public function decrement($keyName = null, $value = 1)
    {
        $redis = $this->getRedis();

        if ($keyName === null) {
            $lastKey = $this->_lastKey;
        } else {
            $lastKey = $this->getKeyName($keyName);
        }

        return $redis->decrBy($lastKey, $value);
    }

    /**
     * {@inheritdoc}
     *
     * @return bool
     */
    public function flush()
    {

    }

    /**
     * Get Prefix
     *
     * @return string
     */
    public function getPrefix()
    {
        return $this->_prefix;
    }

    /**
     * Get Redis Connection
     *
     * @return \Redis
     */
    public function getRedis()
    {
        $redis = $this->_redis;

        if (!is_object($redis)) {
            $this->_connect();
            $redis = $this->_redis;
        }

        return $redis;
    }

    /**
     * Get Key Name
     *
     * @param $keyName
     * @return string
     */
    protected function getKeyName($keyName)
    {
        return $this->_prefix . $keyName;
    }

}