Examples of common methods for PHP to operate Redis database

  • 2021-11-01 02:39:53
  • OfStack

This paper describes the common methods of PHP operating Redis database with examples. Share it for your reference, as follows:

Redis is an open source ANSI C language written, network-supported, memory-based and persistent log-type, Key-Value database, and provides API in multiple languages.

The data types supported by Redis are Stirng (string), List (list), Hash (dictionary), Set (set), Sorted Set (ordered set);

redis version is Redis 2.6. 12 system is Windows+Apache2.4 +php5.6

Connection:


// Instantiation redis
$redis = new Redis();
// Connect 
$redis->connect('127.0.0.1', 6379);
// Check whether the connection was successful 
echo "Server is running: " . $redis->ping();
//  Output result  Server is running: +PONG

Strng (string):


//  Settings 1 The value of a string 
$redis->set('cat', 111);
// Get 1 The value of a string 
echo $redis->get('cat'); // 111
//  Repetition set
$redis->set('cat', 222);
echo $redis->get('cat'); // 222

List (list):


// List 
// Store data in a list 
$redis->lpush('list', 'html');
$redis->lpush('list', 'css');
$redis->lpush('list', 'php');
// Get all the values in the list 
$list = $redis->lrange('list', 0, -1);
print_r($list);echo '<br>'; 
// Add from the right 1 A 
$redis->rpush('list', 'mysql');
$list = $redis->lrange('list', 0, -1);
print_r($list);echo '<br>';
// Eject from left side 1 A 
$redis->lpop('list');
$list = $redis->lrange('list', 0, -1);
print_r($list);echo '<br>';
// Eject from the right 1 A 
$redis->rpop('list');
$list = $redis->lrange('list', 0, -1);
print_r($list);echo '<br>';
//  Results 
// Array ( [0] => php [1] => css [2] => html )
// Array ( [0] => php [1] => css [2] => html [3] => mysql )
// Array ( [0] => css [1] => html [2] => mysql )
// Array ( [0] => css [1] => html )


<?php
  // Instantiation redis
  $redis = new Redis();
  // Connect 
  $redis->connect('127.0.0.1', 6379);
  // List 
  // Store data in a list 
  $redis->lpush('list', 'html');
  $redis->lpush('list', 'css');
  $redis->lpush('list', 'php');
  $redis->lpush('list', 'mysql');
  $redis->lpush('list', 'javascript');
  $redis->lpush('list', 'ajax');
  // Get all the values in the list 
  $list = $redis->lrange('list', 0, -1);
  print_r($list);echo '<br>'; 
  // Gets the length of the list 
  $length = $redis->lsize('list');
  echo $length;echo '<br>';
  // Return list key Medium index The value of the position 
  echo $redis->lget('list', 2);echo '<br>';
  echo $redis->lindex('list', 2);echo '<br>';
  // Settings list index The value of the position 
  echo $redis->lset('list', 2, 'linux');echo '<br>';
  $list = $redis->lrange('list', 0, -1);
  print_r($list);echo '<br>';
  // Return key From start To end Elements between positions 
  $list = $redis->lrange('list', 0, 2);
  print_r($list);echo '<br>';
  $list = $redis->lgetrange('list', 0, 2);
  print_r($list);echo '<br>';
  // Intercept the linked list start To end Elements of 
// The list changes after intercepting the list , List retains intercepted elements , Remaining deletions 
  $list = $redis->ltrim('list', 0, 1);
  print_r($list);echo '<br>';
  $list = $redis->lrange('list', 0, -1);
  print_r($list);echo '<br>';
  //  Results 
  // Array ( [0] => ajax [1] => javascript [2] => mysql [3] => php [4] => css [5] => html )
  // 6
  // mysql
  // mysql
  // 1
  // Array ( [0] => ajax [1] => javascript [2] => linux [3] => php [4] => css [5] => html )
  // Array ( [0] => ajax [1] => javascript [2] => linux )
  // Array ( [0] => ajax [1] => javascript [2] => linux )
  // 1
  // Array ( [0] => ajax [1] => javascript )


<?php
  // Instantiation redis
  $redis = new Redis();
  // Connect 
  $redis->connect('127.0.0.1', 6379);
  // List 
  // Store data in a list 
  $redis->lpush('list', 'html');
  $redis->lpush('list', 'html');
  $redis->lpush('list', 'html');
  $redis->lpush('list', 'css');
  $redis->lpush('list', 'php');
  $redis->lpush('list', 'mysql');
  $redis->lpush('list', 'javascript');
  $redis->lpush('list', 'html');
  $redis->lpush('list', 'html');
  $redis->lpush('list', 'html');
  $redis->lpush('list', 'ajax');
  // Get all the values in the list 
  $list = $redis->lrange('list', 0, -1);
  print_r($list);echo '<br>'; 
  // Delete the list count The value is value Elements of 
  // Delete from left to right 
  $redis->lrem('list', 'html', 2);
  $list = $redis->lrange('list', 0, -1);
  print_r($list);echo '<br>'; 
  // Delete from right to left 
  $redis->lrem('list', 'html', -2);
  $list = $redis->lrange('list', 0, -1);
  print_r($list);echo '<br>'; 
  // Delete all 
  $redis->lrem('list', 'html', 0);
  $list = $redis->lrange('list', 0, -1);
  print_r($list);echo '<br>';
  //  Results 
  // Array ( [0] => ajax [1] => html [2] => html [3] => html [4] => javascript [5] => mysql [6] => php [7] => css [8] => html [9] => html [10] => html )
  // Array ( [0] => ajax [1] => html [2] => javascript [3] => mysql [4] => php [5] => css [6] => html [7] => html [8] => html )
  // Array ( [0] => ajax [1] => html [2] => javascript [3] => mysql [4] => php [5] => css [6] => html )
  // Array ( [0] => ajax [1] => javascript [2] => mysql [3] => php [4] => css )

Hash (dictionary):


<?php
  // Instantiation redis
  $redis = new Redis();
  // Connect 
  $redis->connect('127.0.0.1', 6379);
  // Dictionary 
  // To hash One in the table key Settings value
  // If not, the setting is successful , Return 1, If it exists, it will replace the original value , Return 0, Failure return 0
  echo $redis->hset('hash', 'cat', 'cat');echo '<br>';
  echo $redis->hset('hash', 'cat', 'cat');echo '<br>';
  echo $redis->hset('hash', 'cat', 'cat1');echo '<br>';
  echo $redis->hset('hash', 'dog', 'dog');echo '<br>';
  echo $redis->hset('hash', 'bird', 'bird');echo '<br>';
  echo $redis->hset('hash', 'monkey', 'monkey');echo '<br>';
  // Get hash One of the key Value of 
  echo $redis->hget('hash', 'cat');echo '<br>';
  // Get hash All of the keys
  $arr = $redis->hkeys('hash');
  print_r($arr);echo '<br>';
  // Get hash All the values in the   The order is random 
  $arr = $redis->hvals('hash');
  print_r($arr);echo '<br>';
  // Get 1 A hash All of the key And value  The order is random 
  $arr = $redis->hgetall('hash');
  print_r($arr);echo '<br>';
  // Get hash Medium key Quantity of 
  echo $redis->hlen('hash');echo '<br>';
  // Delete hash Medium 1 A key  If the table does not exist or key Returns if it does not exist false
  echo $redis->hdel('hash', 'dog');echo '<br>';
  var_dump($redis->hdel('hash', 'rabbit'));echo '<br>';
  //  Results 
  // 1
  // 0
  // 0
  // 1
  // 1
  // 1
  // cat1
  // Array ( [0] => cat [1] => dog [2] => bird [3] => monkey )
  // Array ( [0] => cat1 [1] => dog [2] => bird [3] => monkey )
  // Array ( [cat] => cat1 [dog] => dog [bird] => bird [monkey] => monkey )
  // 4
  // 1
  // int(0)


<?php
  // Instantiation redis
  $redis = new Redis();
  // Connect 
  $redis->connect('127.0.0.1', 6379);
  // Dictionary 
  // Batch setting of multiple key Value of 
  $arr = [1=>1, 2=>2, 3=>3, 4=>4, 5=>5];
  $redis->hmset('hash', $arr);
  print_r($redis->hgetall('hash'));echo '<br>';
  //  There are multiple batch acquisition amounts key Value of 
  $arr = [1, 2, 3, 5];
  $hash = $redis->hmget('hash', $arr);
  print_r($hash);echo '<br>';
  // Detection hash One of the key Know whether it exists 
  echo $redis->hexists('hash', '1');echo '<br>';
  var_dump($redis->hexists('hash', 'cat'));echo '<br>';
  print_r($redis->hgetall('hash'));echo '<br>';
  // To hash In the table key Increase 1 Integer value 
  $redis->hincrby('hash', '1', 1);
  print_r($redis->hgetall('hash'));echo '<br>';
  // To hash One of the key Increase 1 Floating point value 
  $redis->hincrbyfloat('hash', 2, 1.3);
  print_r($redis->hgetall('hash'));echo '<br>';
  // Results 
  // Array ( [1] => 1 [2] => 2 [3] => 3 [4] => 4 [5] => 5 )
  // Array ( [1] => 1 [2] => 2 [3] => 3 [5] => 5 )
  // 1
  // bool(false)
  // Array ( [1] => 1 [2] => 2 [3] => 3 [4] => 4 [5] => 5 )
  // Array ( [1] => 2 [2] => 2 [3] => 3 [4] => 4 [5] => 5 )
  // Array ( [1] => 2 [2] => 3.3 [3] => 3 [4] => 4 [5] => 5 )

Set (Set):


<?php
  // Instantiation redis
  $redis = new Redis();
  // Connect 
  $redis->connect('127.0.0.1', 6379);
  // Set 
  //  Add 1 Elements 
  echo $redis->sadd('set', 'cat');echo '<br>';
  echo $redis->sadd('set', 'cat');echo '<br>';
  echo $redis->sadd('set', 'dog');echo '<br>';
  echo $redis->sadd('set', 'rabbit');echo '<br>';
  echo $redis->sadd('set', 'bear');echo '<br>';
  echo $redis->sadd('set', 'horse');echo '<br>';
  //  View all the elements in the collection 
  $set = $redis->smembers('set');
  print_r($set);echo '<br>';
  // Deletes the value
  echo $redis->srem('set', 'cat');echo '<br>';
  var_dump($redis->srem('set', 'bird'));echo '<br>';
  $set = $redis->smembers('set');
  print_r($set);echo '<br>';
  // Determine whether the element is set Members of 
  var_dump($redis->sismember('set', 'dog'));echo '<br>';
  var_dump($redis->sismember('set', 'bird'));echo '<br>';
  // View the number of members in the collection 
  echo $redis->scard('set');echo '<br>';
  // Removes and returns the 1 Random elements ( Returns the removed element )
  echo $redis->spop('set');echo '<br>';
  print_r($redis->smembers('set'));echo '<br>';
  //  Results 
  // 1
  // 0
  // 1
  // 1
  // 1
  // 1
  // Array ( [0] => rabbit [1] => cat [2] => bear [3] => dog [4] => horse )
  // 1
  // int(0)
  // Array ( [0] => dog [1] => rabbit [2] => horse [3] => bear )
  // bool(true)
  // bool(false)
  // 4
  // bear
  // Array ( [0] => dog [1] => rabbit [2] => horse )


<?php
  // Instantiation redis
  $redis = new Redis();
  // Connect 
  $redis->connect('127.0.0.1', 6379);
  // Set 
  $redis->sadd('set', 'horse');
  $redis->sadd('set', 'cat');
  $redis->sadd('set', 'dog');
  $redis->sadd('set', 'bird');
  $redis->sadd('set2', 'fish');
  $redis->sadd('set2', 'dog');
  $redis->sadd('set2', 'bird');
  print_r($redis->smembers('set'));echo '<br>';
  print_r($redis->smembers('set2'));echo '<br>';
  // Returns the intersection of a collection 
  print_r($redis->sinter('set', 'set2'));echo '<br>';
  // Perform intersection operation   And the results are put into 1 In a set of 
  $redis->sinterstore('output', 'set', 'set2');
  print_r($redis->smembers('output'));echo '<br>';
  // Returns the union of a set 
  print_r($redis->sunion('set', 'set2'));echo '<br>';
  // Perform union operation   And the results are put into 1 In a set of 
  $redis->sunionstore('output', 'set', 'set2');
  print_r($redis->smembers('output'));echo '<br>';
  // Returns the difference set of the set 
  print_r($redis->sdiff('set', 'set2'));echo '<br>';
  // Perform difference set operation   And the results are put into 1 In a set of 
  $redis->sdiffstore('output', 'set', 'set2');
  print_r($redis->smembers('output'));echo '<br>';
  //  Results 
  // Array ( [0] => cat [1] => dog [2] => bird [3] => horse )
  // Array ( [0] => bird [1] => dog [2] => fish )
  // Array ( [0] => bird [1] => dog )
  // Array ( [0] => dog [1] => bird )
  // Array ( [0] => cat [1] => dog [2] => bird [3] => horse [4] => fish )
  // Array ( [0] => cat [1] => dog [2] => bird [3] => horse [4] => fish )
  // Array ( [0] => horse [1] => cat )
  // Array ( [0] => horse [1] => cat )

Sorted Set (Ordered Set):


<?php
  // Instantiation redis
  $redis = new Redis();
  // Connect 
  $redis->connect('127.0.0.1', 6379);
  // Ordered set 
  // Add Element 
  echo $redis->zadd('set', 1, 'cat');echo '<br>';
  echo $redis->zadd('set', 2, 'dog');echo '<br>';
  echo $redis->zadd('set', 3, 'fish');echo '<br>';
  echo $redis->zadd('set', 4, 'dog');echo '<br>';
  echo $redis->zadd('set', 4, 'bird');echo '<br>';
  // Returns all elements in the collection 
  print_r($redis->zrange('set', 0, -1));echo '<br>';
  print_r($redis->zrange('set', 0, -1, true));echo '<br>';
  // Returns the of the element score Value 
  echo $redis->zscore('set', 'dog');echo '<br>';
  // Returns the number of stores 
  echo $redis->zcard('set');echo '<br>';
  // Delete the specified member 
  $redis->zrem('set', 'cat');
  print_r($redis->zrange('set', 0, -1));echo '<br>';
  // Returns the collection mediation in the min And max Number of values between 
  print_r($redis->zcount('set', 3, 5));echo '<br>';
  // Returns an ordered set score Between min And max Value between 
  print_r($redis->zrangebyscore('set', 3, 5));echo '<br>';
  print_r($redis->zrangebyscore('set', 3, 5, ['withscores'=>true]));echo '<br>';
  // Returns all values in the specified interval in the collection 
  print_r($redis->zrevrange('set', 1, 2));echo '<br>';
  print_r($redis->zrevrange('set', 1, 2, true));echo '<br>';
  // Object of the specified value in the ordered collection socre Increase 
  echo $redis->zscore('set', 'dog');echo '<br>';
  $redis->zincrby('set', 2, 'dog');
  echo $redis->zscore('set', 'dog');echo '<br>';
  // Remove score Value between min And max Elements between 
  print_r($redis->zrange('set', 0, -1, true));echo '<br>';
  print_r($redis->zremrangebyscore('set', 3, 4));echo '<br>';
  print_r($redis->zrange('set', 0, -1, true));echo '<br>';
  // Results 
  // 1
  // 0
  // 0
  // 0
  // 0
  // Array ( [0] => cat [1] => fish [2] => bird [3] => dog )
  // Array ( [cat] => 1 [fish] => 3 [bird] => 4 [dog] => 4 )
  // 4
  // 4
  // Array ( [0] => fish [1] => bird [2] => dog )
  // 3
  // Array ( [0] => fish [1] => bird [2] => dog )
  // Array ( [fish] => 3 [bird] => 4 [dog] => 4 )
  // Array ( [0] => bird [1] => fish )
  // Array ( [bird] => 4 [fish] => 3 )
  // 4
  // 6
  // Array ( [fish] => 3 [bird] => 4 [dog] => 6 )
  // 2
  // Array ( [dog] => 6 )

For more readers interested in PHP related contents, please check the topics on this site: "Summary of php+redis Database Programming Skills", "Introduction to php Object-Oriented Programming", "Introduction to PHP Basic Syntax", "Encyclopedia of PHP Array (Array) Operation Skills", "Summary of php String (string) Usage", "Introduction to php+mysql Database Operation Skills" and "Summary of php Common Database Operation Skills"

I hope this article is helpful to everyone's PHP programming.


Related articles: