Home>Article>Web Front-end> Redis
Redis is a NOSQL database in which data is stored in key-value pairs.
Redis stores data on main memory meaning its fast but its volatile unlike database systems like PostgreSQL which stores data on disk.
In an application with a server and a database, accessing information on the database will cause latency.
Key-Value databases like redis is often kept in front of those databases and access operations takes 2 steps:
With these 2 steps and given that main memory has lower latency than disk, we can say that redis can be used to store and access frequent, recent data and expensive operations thereby making applications 10 to 50x faster.
It is thereby a fact that if at all you are going to building an application that is going to be scalable in the future, Redis(or databases similar to Redis) is indispensable.
Is Redis Open Source?: Yes, Redis is open source with a special kind of license but there are alternatives like KeyDB by Snapchat and Memcached that are open source.KeyDBis a fork of Redis but as of now can only run on Linux.
Does Redis Support Windows?: No, Redis does not support Windows, but you can install WSL/Ubuntu and install redis using
sudo apt install redis
redis-server --port 6000
where 6000 is the port on your local server you want to run it on. (i.e. 127.0.0.1:6000).
Most issues are due to the fact that the default port that redis-server is using is already being used
On Linux, installing redis is as simple as
sudo apt install redis
oron Mac:
brew install redis
You need to have2 instances of your terminalon
on the first one to run your server
redis-server -port
and on the second one to run your client
redis-cli
When building a web app, you dont need to run the clientbut the server needs to be running
Redis Concepts
There are 3 main concepts in redis you should understandor 4
All other data structures are mainly an advanced implementation of the first
Bylist, I meantarraynotlinked listand thats what redis calls it.
Key-Value pairs are a common data structure in most programming languages.
In Python it is called dictionaries ordictin short
In Javascript it is calledMaps
In C# it is calledDictionaries
There are 6 main operations for key-value pairs in Redis
Operations | Explanations | Examples | Further Explanation |
---|---|---|---|
SET | Creates a key-value pair | SET name John | Sets key to map to value |
GET | Gets value for key | GET name | This returns John |
DEL | Deletes the key value pair | DEL name | This removesthe key value pair name from the database |
EXISTS | Checks if a key exists in the redis database | EXISTS name | returns 1 or 0 corresponding to True or False |
FLUSHALL | Clears the entire cache | FLUSHALL | more or less like DROP TABLE in postgres |
KEYS * | Returns all the keys in the database | KEYS * |
時間ベースの操作もあります。時間ベースの操作では、キーは設定された期間だけキャッシュ内にあり、時間が経過するとキャッシュはキーを削除します。
主に 2 つの時間ベースの操作があります
操作 | 説明 | 例 | さらなる説明 |
---|---|---|---|
期限切れ | 定義後にキーを期限切れにする | 期限切れの名前 10 | 10 秒後にキーと値の名前を削除します。キーは EXPIRE を呼び出す前に SET で定義する必要があります。一般的な代替案は次のとおりです: |
セテックス | 定義時にキーを期限切れにする | セテックス 年齢 30 15 | キーの有効期間を値 30 に設定し、その後 15 秒後にキーと値のペアを削除します |
TTL | 生存時間 | TTL年齢 | 年齢キーが削除されるまでの残り時間を返します |
ブログが長くなりすぎました。リスト、セット、ハッシュマップ、そしてredisに関する面接の質問については別のブログでお話します
ご質問がございましたら、下にコメントしてください。できる限りお答えいたします。⭐ハッピーコーディング
The above is the detailed content of Redis. For more information, please follow other related articles on the PHP Chinese website!