What you must master when you are a beginner in redis?

Time:2020-9-18

Welcome to learn and exchange: PHP advanced learning
Redis can also be written in a variety of open-source languages, such as ANSI key, persistent database, and can be written in a variety of languages.

Redis open source document[ http://www.redis.cn/documenta… ] http://www.redis.cn/documenta…

Set redis password

~~~

vi /usr/local/redis/etc/redis.conf

About 500 lines

requirepass 123456

~~~

Redis supports data types such as string, list, hash, set and sorted set;

connect:

~~~

//Instantiating redis

$redis = new Redis();

//Connection

$redis->connect(‘127.0.0.1’, 6379);

//Check whether the connection is successful

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

//The output result server is running: + Pong

~~~

String:

~~~

//Set the value of a string

$redis->set(‘cat’, 111);

//Gets the value of a string

echo $redis->get(‘cat’); // 111

//Repeat set

$redis->set(‘cat’, 222);

echo $redis->get(‘cat’); // 222

~~~

List:

~~~

//Store data in list

$redis->lpush(‘list’, ‘html’);

$redis->lpush(‘list’, ‘css’);

$redis->lpush(‘list’, ‘php’);

//Gets 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 ‘
‘;

//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

//Instantiating redis

$redis = new Redis();

//Connection

$redis->connect(‘127.0.0.1’, 6379);

//List

//Store data in 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’);

//Gets all the values in the list

$list = $redis->lrange(‘list’, 0, -1);

print_r($list);echo ‘
‘;

//Gets the length of the list

$length = $redis->lsize(‘list’);

echo $length;echo ‘
‘;

//Returns 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 ‘
‘;

//Returns the element from start to end 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

//After intercepting the list, the list changes. The list retains the intercepted elements and the rest is deleted

$list = $redis->ltrim(‘list’, 0, 1);

print_r($list);echo ‘
‘;

$list = $redis->lrange(‘list’, 0, -1);

print_r($list);echo ‘
‘;

//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

//Instantiating redis

$redis = new Redis();

//Connection

$redis->connect(‘127.0.0.1’, 6379);

//List

//Store data in 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’);

//Gets all the values in the list

$list = $redis->lrange(‘list’, 0, -1);

print_r($list);echo ‘
‘;

//Delete count elements with value in the list

//Delete left to right

$redis->lrem(‘list’, ‘html’, 2);

$list = $redis->lrange(‘list’, 0, -1);

print_r($list);echo ‘
‘;

//Delete 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 ‘
‘;

//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:

~~~

<?php

//Instantiating redis

$redis = new Redis();

//Connection

$redis->connect(‘127.0.0.1’, 6379);

//Dictionary

//Set value to a key in the hash table

//If it is not set successfully, it will return 1. If it exists, it will replace the original value and return 0. If it fails, it will 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 ‘
‘;

//The order of getting all the values in the hash is random

$arr = $redis->hvals(‘hash’);

print_r($arr);echo ‘
‘;

//The order of getting all the keys and values in a hash is random

$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, false is returned

echo $redis->hdel(‘hash’, ‘dog’);echo ‘
‘;

var_dump($redis->hdel(‘hash’, ‘rabbit’));echo ‘
‘;

//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

//Instantiating redis

$redis = new Redis();

//Connection

$redis->connect(‘127.0.0.1’, 6379);

//Dictionary

//Batch setting multiple key values

$arr = [1=>1, 2=>2, 3=>3, 4=>4, 5=>5];

$redis->hmset(‘hash’, $arr);

print_r($redis->hgetall(‘hash’));echo ‘
‘;

//Batch obtain the value of multiple keys

$arr = [1, 2, 3, 5];

$hash = $redis->hmget(‘hash’, $arr);

print_r($hash);echo ‘
‘;

//Check whether a key exists in the hash

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 ‘
‘;

//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:

~~~

<?php

//Instantiating redis

$redis = new Redis();

//Connection

$redis->connect(‘127.0.0.1’, 6379);

//Assemble

//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 value from collection

echo $redis->srem(‘set’, ‘cat’);echo ‘
‘;

var_dump($redis->srem(‘set’, ‘bird’));echo ‘
‘;

$set = $redis->smembers(‘set’);

print_r($set);echo ‘
‘;

//Determine whether an element is a member of a set

var_dump($redis->sismember(‘set’, ‘dog’));echo ‘
‘;

var_dump($redis->sismember(‘set’, ‘bird’));echo ‘
‘;

//View the number of members in the collection

echo $redis->scard(‘set’);echo ‘
‘;

//Remove and return a random element in the collection (returns the removed element)

echo $redis->spop(‘set’);echo ‘
‘;

print_r($redis->smembers(‘set’));echo ‘
‘;

//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

//Instantiating redis

$redis = new Redis();

//Connection

$redis->connect(‘127.0.0.1’, 6379);

//Assemble

$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 ‘
‘;

//Returns the intersection of a set

print_r($redis->sinter(‘set’, ‘set2’));echo ‘
‘;

//Perform the intersection operation and put the results into a set

$redis->sinterstore(‘output’, ‘set’, ‘set2’);

print_r($redis->smembers(‘output’));echo ‘
‘;

//Returns the union of a set

print_r($redis->sunion(‘set’, ‘set2’));echo ‘
‘;

//The union operation is performed and the results are put into a collection

$redis->sunionstore(‘output’, ‘set’, ‘set2’);

print_r($redis->smembers(‘output’));echo ‘
‘;

//Returns the difference set of a set

print_r($redis->sdiff(‘set’, ‘set2’));echo ‘
‘;

//Perform the subtraction operation and put the results into a set

$redis->sdiffstore(‘output’, ‘set’, ‘set2’);

print_r($redis->smembers(‘output’));echo ‘
‘;

//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:

~~~

<?php

//Instantiating redis

$redis = new Redis();

//Connection

$redis->connect(‘127.0.0.1’, 6379);

//Ordered set

//Add elements

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 ‘
‘;

//Returns all elements in the collection

print_r($redis->zrange(‘set’, 0, -1));echo ‘
‘;

print_r($redis->zrange(‘set’, 0, -1, true));echo ‘
‘;

//Returns the score value of the element

echo $redis->zscore(‘set’, ‘dog’);echo ‘
‘;

//Returns the number of stores

echo $redis->zcard(‘set’);echo ‘
‘;

//Delete specified member

$redis->zrem(‘set’, ‘cat’);

print_r($redis->zrange(‘set’, 0, -1));echo ‘
‘;

//Returns the number of values between min and Max in the collection

print_r($redis->zcount(‘set’, 3, 5));echo ‘
‘;

//Returns the value of score between min and Max in an ordered set

print_r($redis->zrangebyscore(‘set’, 3, 5));echo ‘
‘;

print_r($redis->zrangebyscore(‘set’, 3, 5, [‘withscores’=>true]));echo ‘
‘;

//Returns all values in a specified interval in a collection

print_r($redis->zrevrange(‘set’, 1, 2));echo ‘
‘;

print_r($redis->zrevrange(‘set’, 1, 2, true));echo ‘
‘;

//Socre increase of specified value in ordered set

echo $redis->zscore(‘set’, ‘dog’);echo ‘
‘;

$redis->zincrby(‘set’, 2, ‘dog’);

echo $redis->zscore(‘set’, ‘dog’);echo ‘
‘;

//Remove elements with score values 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 ‘
‘;

//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 )

~~~

Recommended Today

Solutions to leetcode problems and knowledge points 1. Sum of two numbers

Title Link 1. Two Sum  difficulty: $/ color {00965e} {easy}$ Knowledge points 1. Hash (hash) function The classic hash function times33 is widely used, and the core algorithm is as follows: hash(i) = hash(i-1) * 33 + str[i] Laruence has an article about:Hash algorithm in PHP 2. Hash collision processing method 2.1 chain address method […]