PHP operation Redis, primary PHP can't remember to operate several times

Keywords: PHP Redis MySQL Laravel

Basic operation methods of redis

1.redis Connections for:
//Instantiate redis

$redis = new Redis();

//Connect

 $redis->connect('127.0.0.1', 6379);

//Check whether the connection is successful

echo "Server is running: " . $redis->ping();

// Output result Server is running: +PONG

2.redis operation Strng (string):

// Set the value of a string
$redis->set('cat', 111);
//Get the value of a string
echo $redis->get('cat'); // 111
// Repeat set
$redis->set('cat', 222);
echo $redis->get('cat'); // 222 

3.redis operation List:

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

4.redis operation Hash (Dictionary):

<?php
//Instantiate redis
$redis = new Redis();
//Connect
$redis->connect('127.0.0.1', 6379);
//Dictionaries
//Set value for a key in the hash table
//If it is not set, it will return 1. If it is set, it will replace the original value and return 0. If it fails, it will return 0
echo $redis->hset('hash', 'cat', 'cat');echo '<br>';   // 1
echo $redis->hset('hash', 'cat', 'cat');echo '<br>';   // 0
echo $redis->hset('hash', 'cat', 'cat1');echo '<br>';   // 0
echo $redis->hset('hash', 'dog', 'dog');echo '<br>';   // 1
echo $redis->hset('hash', 'bird', 'bird');echo '<br>';   // 1
echo $redis->hset('hash', 'monkey', 'monkey');echo '<br>';   // 1
//Get the value of a key in the hash
echo $redis->hget('hash', 'cat');echo '<br>';  // cat1
//Get all keys in the hash
$arr = $redis->hkeys('hash');
print_r($arr);echo '<br>';
// Array ( [0] => cat [1] => dog [2] => bird [3] => monkey )
//The order of getting all the values in the hash is random
$arr = $redis->hvals('hash');
print_r($arr);echo '<br>';
 // Array ( [0] => cat1 [1] => dog [2] => bird [3] => monkey )
//The order of getting all key s and value s in a hash is random
$arr = $redis->hgetall('hash');
print_r($arr);echo '<br>';
 // Array ( [cat] => cat1 [dog] => dog [bird] => bird [monkey] => monkey )
//Get the number of key s in the hash
echo $redis->hlen('hash');echo '<br>';
 // 4
//Delete a key in the hash and return false if the table does not exist or the key does not exist
echo $redis->hdel('hash', 'dog');echo '<br>';
var_dump($redis->hdel('hash', 'rabbit'));echo '<br>';
// 1
// int(0) 

5.redis operation Set:

<?php
//Instantiate redis
$redis = new Redis();
//Connect
$redis->connect('127.0.0.1', 6379);
//aggregate
// Add an element
echo $redis->sadd('set', 'cat');echo '<br>';         // 1
echo $redis->sadd('set', 'cat');echo '<br>';         // 0
echo $redis->sadd('set', 'dog');echo '<br>';        // 1
echo $redis->sadd('set', 'rabbit');echo '<br>';     // 1
echo $redis->sadd('set', 'bear');echo '<br>';      // 1
echo $redis->sadd('set', 'horse');echo '<br>';    // 1
// View all elements in the collection
$set = $redis->smembers('set');
print_r($set);echo '<br>';
// Array ( [0] => rabbit [1] => cat [2] => bear [3] => dog [4] => horse )
//Delete value from collection
echo $redis->srem('set', 'cat');echo '<br>';    // 1
var_dump($redis->srem('set', 'bird'));echo '<br>';     // int(0)
$set = $redis->smembers('set');
print_r($set);echo '<br>';
// Array ( [0] => dog [1] => rabbit [2] => horse [3] => bear )
//Determine whether the element is a member of set
var_dump($redis->sismember('set', 'dog'));echo '<br>';     // bool(true)
var_dump($redis->sismember('set', 'bird'));echo '<br>';    // bool(false)
//View the number of members in the collection
echo $redis->scard('set');echo '<br>';    // 4
//Remove and return a random element in the collection (return the removed element)
echo $redis->spop('set');echo '<br>';  // bear
print_r($redis->smembers('set'));echo '<br>';  
 // Array ( [0] => dog [1] => rabbit [2] => horse )
<?php
//Instantiate redis
$redis = new Redis();
//Connect
$redis->connect('127.0.0.1', 6379);
//aggregate
$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>';
 // Array ( [0] => cat [1] => dog [2] => bird [3] => horse )
print_r($redis->smembers('set2'));echo '<br>';
// Array ( [0] => bird [1] => dog [2] => fish )
//Returns the intersection of sets
print_r($redis->sinter('set', 'set2'));echo '<br>';
// Array ( [0] => dog [1] => bird )
//Perform the intersection operation and put the result into a collection
$redis->sinterstore('output', 'set', 'set2');
print_r($redis->smembers('output'));echo '<br>';
// Array ( [0] => dog [1] => bird )
//Returns the union of a collection
print_r($redis->sunion('set', 'set2'));echo '<br>';
// Array ( [0] => cat [1] => dog [2] => bird [3] => horse [4] => fish )
//Performs a union operation and places the result in a collection
$redis->sunionstore('output', 'set', 'set2');
print_r($redis->smembers('output'));echo '<br>';
 // Array ( [0] => cat [1] => dog [2] => bird [3] => horse [4] => fish )
//Returns the difference set of a set
print_r($redis->sdiff('set', 'set2'));echo '<br>';
// Array ( [0] => horse [1] => cat )
//Perform a subtraction operation and place the result in a collection
$redis->sdiffstore('output', 'set', 'set2');
print_r($redis->smembers('output'));echo '<br>';
// Array ( [0] => horse [1] => cat ) 

6.redis operation Sorted Set:

<?php
//Instantiate redis
$redis = new Redis();
//Connect
$redis->connect('127.0.0.1', 6379);
//Ordered set
//Add element score
echo $redis->zadd('set', 1, 'cat');echo '<br>';      // 1
echo $redis->zadd('set', 2, 'dog');echo '<br>';    // 1
echo $redis->zadd('set', 3, 'fish');echo '<br>';    // 1
echo $redis->zadd('set', 4, 'dog');echo '<br>';    // 0
echo $redis->zadd('set', 4, 'bird');echo '<br>';    // 1
//Returns all elements in the collection
print_r($redis->zrange('set', 0, -1));echo '<br>';
// Array ( [0] => cat [1] => fish [2] => bird [3] => dog )
print_r($redis->zrange('set', 0, -1, true));echo '<br>';
// Array ( [cat] => 1 [fish] => 3 [bird] => 4 [dog] => 4 )
//Returns the score value of the element
echo $redis->zscore('set', 'dog');echo '<br>';
// 4
//Return the number of stores
echo $redis->zcard('set');echo '<br>';
// 4
//Delete specified member
$redis->zrem('set', 'cat');
print_r($redis->zrange('set', 0, -1));echo '<br>';
// Array ( [0] => fish [1] => bird [2] => dog )
//Returns the number of values intermediate between min and max in a set
print_r($redis->zcount('set', 3, 5));echo '<br>';
// 3
//Returns the value of score between min and max in an ordered set
print_r($redis->zrangebyscore('set', 3, 5));echo '<br>';
 // Array ( [0] => fish [1] => bird [2] => dog )
print_r($redis->zrangebyscore('set', 3, 5, ['withscores'=>true]));echo '<br>';
 // Array ( [fish] => 3 [bird] => 4 [dog] => 4 )
//Returns the flashback of all values in a specified interval in a set
print_r($redis->zrevrange('set', 1, 2));echo '<br>';
 // Array ( [0] => bird [1] => fish )
print_r($redis->zrevrange('set', 1, 2, true));echo '<br>';
// Array ( [bird] => 4 [fish] => 3 )
//socre increase of specified value in ordered set
echo $redis->zscore('set', 'dog');echo '<br>';
 // 4
$redis->zincrby('set', 2, 'dog');
echo $redis->zscore('set', 'dog');echo '<br>';
// 6
//Remove elements with score values between min and max
print_r($redis->zrange('set', 0, -1, true));echo '<br>';
// Array ( [fish] => 3 [bird] => 4 [dog] => 6 )
print_r($redis->zremrangebyscore('set', 3, 4));echo '<br>';
 // 2
print_r($redis->zrange('set', 0, -1, true));echo '<br>';
 // Array ( [dog] => 6 )
//Result 

phper always encounters some problems and bottlenecks when it is advanced. There is no sense of direction when it writes more business code. I don't know where to start to improve. For this, I collated some data, including but not limited to: distributed architecture, high scalability, high performance, high concurrency, server performance tuning, TP6, laravel, YII2, Redis, Swoole, Kafka, Mysql optimization, shell script, D Ocker, microservice, Nginx and other advanced advanced dry goods can be shared for free Please poke here.

Posted by the-hardy-kid on Sat, 02 Nov 2019 00:25:09 -0700