Yii2 Framework redis Basic Application Example

  • 2021-10-25 06:04:17
  • OfStack

This paper describes the basic application of Yii2 framework redis with examples. Share it for your reference, as follows:

When applying yii2, it is necessary to extend and install it first

If composer is installed, it will run directly


php composer.phar require --prefer-dist yiisoft/yii2-redis

Of course, it can also be installed locally

Download and unzip the yii2-redis expansion pack (https://github.com/yiisoft/yii2-redis)

Move the extracted file to vebdor/yiisoft named yii2-redis

Open extensions. php under vebdor/yiisoft

Add the following code


'yiisoft/yii2-redis' =>
 array (
  'name' => 'yiisoft/yii2-redis',
  'version' => '2.0.5.0',
  'alias' =>
  array (
   '@yii/redis' => $vendorDir . '/yiisoft/yii2-redis',
  ),
 ),

Finally, add the following configuration item in web. php under config file (the configuration file directory should be written correctly because this was a mistake and wasted 1 morning time)


'redis' =>[
      'class' => 'yii\redis\Connection',
      'hostname' => 'localhost', // Yours redis Address 
      'port' => 6379, // Port 
      'database' => 0,
    ]

Next, you can operate on redis

Here are a few simple small examples

1) Set key values


$source = Yii::$app->redis->set('var1','asdasd');

Returns a Boolean value on success


$source = Yii::$app->redis->get('var1');

Gets the value of the key


$source = Yii::$app->redis->del('var1');

Delete key


$var2 = Yii::$app->redis->keys("*");

View all keys

2) List

Settings list


$var1 = Yii::$app->redis->lpush("vari","lisr");

Take out the list (paragraph 1)


$var3 = Yii::$app->redis->lrange("vari",0,2);

Modifying the value of an index of a list


$var33 = Yii::$app->redis->lset('vari',21,'2323');

3) Hash

Suitable for storing objects

Store hash


'yiisoft/yii2-redis' =>
 array (
  'name' => 'yiisoft/yii2-redis',
  'version' => '2.0.5.0',
  'alias' =>
  array (
   '@yii/redis' => $vendorDir . '/yiisoft/yii2-redis',
  ),
 ),

0

Read hash


'yiisoft/yii2-redis' =>
 array (
  'name' => 'yiisoft/yii2-redis',
  'version' => '2.0.5.0',
  'alias' =>
  array (
   '@yii/redis' => $vendorDir . '/yiisoft/yii2-redis',
  ),
 ),

1

4) Collections

Set collection


'yiisoft/yii2-redis' =>
 array (
  'name' => 'yiisoft/yii2-redis',
  'version' => '2.0.5.0',
  'alias' =>
  array (
   '@yii/redis' => $vendorDir . '/yiisoft/yii2-redis',
  ),
 ),

2

Gets the number of members of the collection


$var60 = Yii::$app->redis->scard('mioji1');

Gets the members of the collection


'yiisoft/yii2-redis' =>
 array (
  'name' => 'yiisoft/yii2-redis',
  'version' => '2.0.5.0',
  'alias' =>
  array (
   '@yii/redis' => $vendorDir . '/yiisoft/yii2-redis',
  ),
 ),

4

5) Ordered sets

Setting indexes and members in a collection


'yiisoft/yii2-redis' =>
 array (
  'name' => 'yiisoft/yii2-redis',
  'version' => '2.0.5.0',
  'alias' =>
  array (
   '@yii/redis' => $vendorDir . '/yiisoft/yii2-redis',
  ),
 ),

5

Returns the number of members in the collection


'yiisoft/yii2-redis' =>
 array (
  'name' => 'yiisoft/yii2-redis',
  'version' => '2.0.5.0',
  'alias' =>
  array (
   '@yii/redis' => $vendorDir . '/yiisoft/yii2-redis',
  ),
 ),

6

Returns the members in the specified index interval


'yiisoft/yii2-redis' =>
 array (
  'name' => 'yiisoft/yii2-redis',
  'version' => '2.0.5.0',
  'alias' =>
  array (
   '@yii/redis' => $vendorDir . '/yiisoft/yii2-redis',
  ),
 ),

7

6) Publish a subscription


$var7 = Yii::$app->redis->psubscribe('redisChat');

This can realize the function of chat room, but it still needs polling (I won't introduce it for the time being)

For more readers interested in Yii related content, please check the topics on this site: "Introduction to Yii Framework and Summary of Common Skills", "Summary of Excellent Development Framework of php", "Introduction to smarty Template", "Introduction to php Object-Oriented Programming", "Summary of Usage of php String (string)", "Introduction to php+mysql Database Operation" and "Summary of Common Database Operation Skills of php"

I hope this article is helpful to the PHP programming based on Yii framework.


Related articles: