Can the Yii2 framework redis key prefix be configured?

scenario
because redis is shared with other parts. In addition to specifying the library, we also need to prefix all key of our project with a webapp: prefix.
I now configure a "redis.prefix" = >" webapp:"

in params.php.

then use `Yii::$app- > paramas ["redis.prefix"] in the business code. Although I can, I always feel uncomfortable.

then I would like to ask if there is any configuration or transparent key storage in yii2, and then automatically add the prefix webapp:

.

Thank you.

Mar.15,2021

< H2 > 1. If you simply use Redis as a cache component: < / H2 >
'components' => [
    'cache' => [
        'class' => 'yii\redis\Cache',
    ],
],

just add the keyPrefix attribute to the configuration file:

'components' => [
    'cache' => [
        'class' => 'yii\redis\Cache',
        'keyPrefix' => 'webapp:'
    ],
]
< hr > < H2 > 2. If you are using Redis components directly: < / H2 >
'components' => [
    'redis' => [
        'class' => 'yii\redis\Connection',
        'hostname' => 'localhost',
        'port' => 6379,
        'database' => 0,
    ],
],

in this way, there is no way to add prefixes automatically, you can only do it yourself.

if it were me, I would write a separate helper class for Redis, calling only the \ Yii::$app- > redis component in this Redis helper class.

Redis basic class, put some basic methods, each data type is applicable, each data type is written a subclass, inheriting this class

class RedisBase
{
    /**
     *  key 
     * key
     * @param $key
     * @return string
     */
    protected static function buildKey($key)
    {
        return \Yii::$app->params['redis.prefix'] . $key;
    }

    /**
     * @return \yii\redis\Connection
     */
    protected static function getRedis()
    {
        return \Yii::$app->redis;
    }

    /**
     * -sharp exists key
     *
     * key
     * @param string $key
     * @return bool
     */
    public static function exists($key)
    {
        $key = self::buildKey($key);
        $redis = self::getRedis();
        return (bool) $redis->exists($key);
    }

    /**
     * -sharp del key [key2 key3 ]
     *
     * 
     *
     * @param string $key
     * @param bool $strict
     * 
     * true: false
     * false: true
     * @return bool
     */
    public static function del($key, $strict = false)
    {
        $key = self::buildKey($key);

        // ,true
        if (! $strict && ! self::exists($key)) return true;

        $redis = self::getRedis();
        return (bool) $redis->del($key);
    }

    /**
     * -sharp expire key seconds
     *
     *  keyseconds
     *
     * @param $key
     * @param $seconds
     * @return bool
     */
    public static function expire($key, $seconds)
    {
        $key = self::buildKey($key);
        $redis = self::getRedis();
        return (bool) $redis->expire($key, $seconds);
    }

    /**
     * -sharp ttl key
     *
     * 0
     * -1 key
     * -2 key
     *
     * key
     * @param $key
     * @return int
     */
    public static function ttl($key)
    {
        $key = self::buildKey($key);
        $redis = self::getRedis();
        return (int) $redis->ttl($key);
    }

    /**
     * -sharp dbsize
     *
     * db 
     * @return int
     */
    public static function dbSize()
    {
        $redis = self::getRedis();
        return (int) $redis->dbsize();
    }

    // ... redis
}

methods of Redis string type, placed separately in the RedisString class, inheriting the RedisBase method

class RedisString extends RedisBase
{

    /**
     * -sharp SET key value options
     *
     *  key 
     *
     * @param $key
     * @param $value
     * @param array $options
     *
     * EX seconds -- key
     * PX milliseconds -- key
     * NX -- key.
     * XX -- key.
     *
     * eg: ['NX', 'EX', '3600']
     *
     * @return bool
     */
    public static function set($key, $value, array $options = [])
    {
        $key = self::buildKey($key);
        $redis = self::getRedis();
        $param = array_merge([$key, $value], $options);
        return (bool) call_user_func_array([$redis, 'set'], $param);
    }

    /**
     * -sharp GET key
     *
     *  key 
     * @param $key
     * @return string
     */
    public static function get($key)
    {
        $key = self::buildKey($key);
        $redis = self::getRedis();
        return $redis->get($key);
    }

    // ...String
}

methods of Redis collection type, placed separately in the RedisSet class, inheriting the RedisBase method

class RedisSet extends RedisBase
{
    /**
     * -sharp SADD key member1 [member2]
     *
     * 
     *
     * @param $key
     * @param $members
     * @return int 
     */
    public static function sAdd($key, $members)
    {
        $key = self::buildKey($key);
        $redis = self::getRedis();

        // php >= 5.6
        return (int) $redis->sadd($key, ...(array) $members);

        // 
//        $params = array_merge([$key], (array) $members);
//        return (int) call_user_func_array([$redis, 'sadd'], $params);
    }

    /**
     * -sharp SREM key member1 [member2]
     *
     * 
     * @param $key
     * @param $members
     * @return int
     */
    public static function sRem($key, $members)
    {
        $key = self::buildKey($key);
        $redis = self::getRedis();

        $params = array_merge([$key], (array) $members);
        return (int) call_user_func_array([$redis, 'srem'], $params);
    }

    /**
     * -sharp SISMEMBER key member
     *
     *  member  key 
     * @param $key
     * @param $member
     * @return bool
     */
    public static function sIsMember($key, $member)
    {
        $key = self::buildKey($key);
        $redis = self::getRedis();

        return (bool) $redis->sismember($key, $member);
    }

    /**
     * -sharp SMEMBERS key
     *
     * 
     * @param $key
     * @return array
     */
    public static function sMembers($key)
    {
        $key = self::buildKey($key);
        $redis = self::getRedis();

        return $redis->smembers($key);
    }

    /**
     * -sharp SCARD key
     *
     * 
     * @param $key
     * @return bool
     */
    public static function sCard($key)
    {
        $key = self::buildKey($key);
        $redis = self::getRedis();

        return (int) $redis->scard($key);
    }
    
    // ...Set

methods of Redis ordered collection types, placed separately in the RedisSortedSet class, inherit the RedisBase method

class RedisSortedSet extends RedisBase
{
    /**
     * -sharp ZADD key score1 member1 [score2 member2]
     *
     * 
     * @param $key
     * @param array $options
     * [
     *     'value1' => score1,
     *     'value2' => score2,
     *     'value3' => score3,
     * ]
     *
     * @return int
     */
    public static function zAdd($key, array $options)
    {
        $key = self::buildKey($key);
        $redis = self::getRedis();
        $param = [$key];

        foreach ($options as $value => $score) {
            $param[] = $score;
            $param[] = $value;
        }

        return (int) call_user_func_array([$redis, 'zadd'], $param);
    }
    
    // ...SortedSet
}

methods of type RedisList, placed separately in the RedisList class, inherit the RedisBase method

class RedisList extends RedisBase
{
    // ...
}

methods of type RedisHash, placed separately in the RedisHash class, inherit the RedisBase method

class RedisHash extends RedisBase
{
    // ...
}
< hr >

advantage of writing like this:

1. Before calling the original method of redis, you can preprocess the data
2. Later maintenance is convenient. If you want to modify it, you only need to modify the method here
3. It is convenient to add some comments
4. This method is more IDE-friendly and can jump directly to the method in this class. If you use \ Yii::$app- > redis everywhere, it will be more difficult to maintain
.

in addition, for some data types of Redis, see Redis Learning Notes-data types and API

Menu