Welcome to study and exchange: php advanced learning
Redis is an open source log-type, key-value database written in ANSI C language, supports network, can be memory-based and persistent, and provides APIs in multiple languages.
Redis open source documentation[http://www.redis.cn/documenta…](http://www.redis.cn/documenta…
set redis password
~~~
vi /usr/local/redis/etc/redis.conf
around 500 lines
requirepass 123456
~~~
The data types supported by Redis are Stirng (string), List (list), Hash (dictionary), Set (collection), Sorted Set (ordered collection);
connect:
~~~
//Instantiate redis
$redis = new Redis();
//connect
$redis->connect(‘127.0.0.1’, 6379);
//Check if the connection is successful
echo “Server is running: ” . $redis->ping();
// Output result Server is running: +PONG
~~~
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
~~~
List (list):
~~~
// store the data in the 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 ‘
‘;
// add one from the right
$redis->rpush(‘list’, ‘mysql’);
$list = $redis->lrange(‘list’, 0, -1);
print_r($list);echo ‘
‘;
// pop one from the left
$redis->lpop(‘list’);
$list = $redis->lrange(‘list’, 0, -1);
print_r($list);echo ‘
‘;
// pop one from the right
$redis->rpop(‘list’);
$list = $redis->lrange(‘list’, 0, -1);
print_r($list);echo ‘
‘;
// result
// 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
//Instantiate redis
$redis = new Redis();
//connect
$redis->connect(‘127.0.0.1’, 6379);
//list
// store the data in the 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 ‘
‘;
//get the length of the list
$length = $redis->lsize(‘list’);
echo $length;echo ‘
‘;
//Return the value of the index position in the list key
echo $redis->lget(‘list’, 2);echo ‘
‘;
echo $redis->lindex(‘list’, 2);echo ‘
‘;
//Set the value of the index position in the list
echo $redis->lset(‘list’, 2, ‘linux’);echo ‘
‘;
$list = $redis->lrange(‘list’, 0, -1);
print_r($list);echo ‘
‘;
//Return the element from start to end position in key
$list = $redis->lrange(‘list’, 0, 2);
print_r($list);echo ‘
‘;
$list = $redis->lgetrange(‘list’, 0, 2);
print_r($list);echo ‘
‘;
//Intercept the elements from start to end in the linked list
//The list changes after intercepting the list, the list retains the intercepted elements, and the rest are deleted
$list = $redis->ltrim(‘list’, 0, 1);
print_r($list);echo ‘
‘;
$list = $redis->lrange(‘list’, 0, -1);
print_r($list);echo ‘
‘;
// result
// 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
//Instantiate redis
$redis = new Redis();
//connect
$redis->connect(‘127.0.0.1’, 6379);
//list
// store the data in the 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 ‘
‘;
//remove count elements in the list whose value is value
// delete from left to right
$redis->lrem(‘list’, ‘html’, 2);
$list = $redis->lrange(‘list’, 0, -1);
print_r($list);echo ‘
‘;
// delete from right to left
$redis->lrem(‘list’, ‘html’, -2);
$list = $redis->lrange(‘list’, 0, -1);
print_r($list);echo ‘
‘;
// delete all
$redis->lrem(‘list’, ‘html’, 0);
$list = $redis->lrange(‘list’, 0, -1);
print_r($list);echo ‘
‘;
// result
// 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
//Instantiate redis
$redis = new Redis();
//connect
$redis->connect(‘127.0.0.1’, 6379);
//dictionary
//Set the value to a key in the hash table
//If not, the setting is successful, return 1, if it exists, it will replace the original value, return 0, if it fails, return 0
echo $redis->hset(‘hash’, ‘cat’, ‘cat’);echo ‘
‘;
echo $redis->hset(‘hash’, ‘cat’, ‘cat’);echo ‘
‘;
echo $redis->hset(‘hash’, ‘cat’, ‘cat1’);echo ‘
‘;
echo $redis->hset(‘hash’, ‘dog’, ‘dog’);echo ‘
‘;
echo $redis->hset(‘hash’, ‘bird’, ‘bird’);echo ‘
‘;
echo $redis->hset(‘hash’, ‘monkey’, ‘monkey’);echo ‘
‘;
//Get the value of a key in the hash
echo $redis->hget(‘hash’, ‘cat’);echo ‘
‘;
//Get all the keys in the hash
$arr = $redis->hkeys(‘hash’);
print_r($arr);echo ‘
‘;
//Get all the values in the hash in random order
$arr = $redis->hvals(‘hash’);
print_r($arr);echo ‘
‘;
//Get all keys and values in a hash in random order
$arr = $redis->hgetall(‘hash’);
print_r($arr);echo ‘
‘;
//Get the number of keys in the hash
echo $redis->hlen(‘hash’);echo ‘
‘;
//Delete a key in the hash If the table does not exist or the key does not exist, return false
echo $redis->hdel(‘hash’, ‘dog’);echo ‘
‘;
var_dump($redis->hdel(‘hash’, ‘rabbit’));echo ‘
‘;
// result
// 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
//Instantiate redis
$redis = new Redis();
//connect
$redis->connect(‘127.0.0.1’, 6379);
//dictionary
//Set the value of multiple keys in batches
$arr = [1=>1, 2=>2, 3=>3, 4=>4, 5=>5];
$redis->hmset(‘hash’, $arr);
print_r($redis->hgetall(‘hash’));echo ‘
‘;
// Get the values of multiple keys in batches
$arr = [1, 2, 3, 5];
$hash = $redis->hmget(‘hash’, $arr);
print_r($hash);echo ‘
‘;
//Check whether a key in the hash exists
echo $redis->hexists(‘hash’, ‘1’);echo ‘
‘;
var_dump($redis->hexists(‘hash’, ‘cat’));echo ‘
‘;
print_r($redis->hgetall(‘hash’));echo ‘
‘;
//Add an integer value to the key in the hash table
$redis->hincrby(‘hash’, ‘1’, 1);
print_r($redis->hgetall(‘hash’));echo ‘
‘;
//Add a floating point value to a key in the hash
$redis->hincrbyfloat(‘hash’, 2, 1.3);
print_r($redis->hgetall(‘hash’));echo ‘
‘;
//result
// 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(collection):
~~~
<?php
//Instantiate redis
$redis = new Redis();
//connect
$redis->connect(‘127.0.0.1’, 6379);
//gather
// add an element
echo $redis->sadd(‘set’, ‘cat’);echo ‘
‘;
echo $redis->sadd(‘set’, ‘cat’);echo ‘
‘;
echo $redis->sadd(‘set’, ‘dog’);echo ‘
‘;
echo $redis->sadd(‘set’, ‘rabbit’);echo ‘
‘;
echo $redis->sadd(‘set’, ‘bear’);echo ‘
‘;
echo $redis->sadd(‘set’, ‘horse’);echo ‘
‘;
// view all elements in the collection
$set = $redis->smembers(‘set’);
print_r($set);echo ‘
‘;
// delete the value from the collection
echo $redis->srem(‘set’, ‘cat’);echo ‘
‘;
var_dump($redis->srem(‘set’, ‘bird’));echo ‘
‘;
$set = $redis->smembers(‘set’);
print_r($set);echo ‘
‘;
// Determine if the element is a member of the set
var_dump($redis->sismember(‘set’, ‘dog’));echo ‘
‘;
var_dump($redis->sismember(‘set’, ‘bird’));echo ‘
‘;
// check the number of members in the collection
echo $redis->scard(‘set’);echo ‘
‘;
//Remove and return a random element in the collection (return the removed element)
echo $redis->spop(‘set’);echo ‘
‘;
print_r($redis->smembers(‘set’));echo ‘
‘;
// result
// 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
//Instantiate redis
$redis = new Redis();
//connect
$redis->connect(‘127.0.0.1’, 6379);
//gather
$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 ‘
‘;
print_r($redis->smembers(‘set2’));echo ‘
‘;
//return the intersection of sets
print_r($redis->sinter(‘set’, ‘set2’));echo ‘
‘;
//Perform the intersection operation and put the result into a set
$redis->sinterstore(‘output’, ‘set’, ‘set2’);
print_r($redis->smembers(‘output’));echo ‘
‘;
//return the union of sets
print_r($redis->sunion(‘set’, ‘set2’));echo ‘
‘;
// Perform a union operation and put the result into a set
$redis->sunionstore(‘output’, ‘set’, ‘set2’);
print_r($redis->smembers(‘output’));echo ‘
‘;
//return the difference of the set
print_r($redis->sdiff(‘set’, ‘set2’));echo ‘
‘;
//Perform the difference operation and put the result into a set
$redis->sdiffstore(‘output’, ‘set’, ‘set2’);
print_r($redis->smembers(‘output’));echo ‘
‘;
// result
// 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:
~~~
<?php
//Instantiate redis
$redis = new Redis();
//connect
$redis->connect(‘127.0.0.1’, 6379);
//sorted collection
//add element
echo $redis->zadd(‘set’, 1, ‘cat’);echo ‘
‘;
echo $redis->zadd(‘set’, 2, ‘dog’);echo ‘
‘;
echo $redis->zadd(‘set’, 3, ‘fish’);echo ‘
‘;
echo $redis->zadd(‘set’, 4, ‘dog’);echo ‘
‘;
echo $redis->zadd(‘set’, 4, ‘bird’);echo ‘
‘;
//return all elements in the collection
print_r($redis->zrange(‘set’, 0, -1));echo ‘
‘;
print_r($redis->zrange(‘set’, 0, -1, true));echo ‘
‘;
//return the score value of the element
echo $redis->zscore(‘set’, ‘dog’);echo ‘
‘;
//return the number stored
echo $redis->zcard(‘set’);echo ‘
‘;
// delete the specified member
$redis->zrem(‘set’, ‘cat’);
print_r($redis->zrange(‘set’, 0, -1));echo ‘
‘;
//return the number of values between min and max in the collection
print_r($redis->zcount(‘set’, 3, 5));echo ‘
‘;
//Return the value of score between min and max in the sorted set
print_r($redis->zrangebyscore(‘set’, 3, 5));echo ‘
‘;
print_r($redis->zrangebyscore(‘set’, 3, 5, [‘withscores’=>true]));echo ‘
‘;
//Return all values in the specified range in the collection
print_r($redis->zrevrange(‘set’, 1, 2));echo ‘
‘;
print_r($redis->zrevrange(‘set’, 1, 2, true));echo ‘
‘;
//The socre of the specified value in the ordered set increases
echo $redis->zscore(‘set’, ‘dog’);echo ‘
‘;
$redis->zincrby(‘set’, 2, ‘dog’);
echo $redis->zscore(‘set’, ‘dog’);echo ‘
‘;
//Remove elements whose score value is between min and max
print_r($redis->zrange(‘set’, 0, -1, true));echo ‘
‘;
print_r($redis->zremrangebyscore(‘set’, 3, 4));echo ‘
‘;
print_r($redis->zrange(‘set’, 0, -1, true));echo ‘
‘;
//result
// 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 )
~~~