Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
May 29, 2022 03:28 pm GMT

Redis : Your db for in memory

Hello everyone knows what is redis, its the most popular in-memory data structure store used as a database, cache, message broker, and streaming engine. It offers great data structures out of the boxes such as strings, hashes, lists, sets, sorted sets with range queries, bitmaps, hyperloglogs, geospatial indexes, and streams.

And out of all things it is a open source software so everyone can take a look at the source code, if you want countribute check out the link

GitHub - redis/redis: Redis is an in-memory database that persists on disk. The data model is
Redis is an in-memory database that persists on disk. The data model is key-value, but many different kind of values
github.com

Today I will show you how to use redis with python to create your database in memory using redis. So lets jump into it.

Requirements
First you need python and redis installed in your machine, best option is to use docker and redis for this tutorial.so deploy your redis docker instance with one command in your terminal:

docker run --name redis -p 6379:6379 -d redis

It will create a redis instance at port 6379. Now its created lets create our python code to store the sata into redis database. For this we need redis library from pip. If you don have it on your machiner install it with this command.

pip install redis
Now lets code

First create a file with anyname , lets say test.py and add this code,

Connect to Redis
The following code creates a connection to Redis using redis-py:

import redis
r = redis.Redis(
host='hostname',
port=port,
password='password')
To adapt this example to your code, replace the following values with your databases values:

In line 4, set host to your databases hostname or IP address
In line 5, set port to your databases port
In line 6, set password to your databases password
Example code for Redis commands
Once connected to Redis, you can read and write data with Redis command functions.

The following code snippet assigns the value bar to the Redis key foo, reads it back, and prints it:

open a connection to Redis

r.set('foo', 'bar')value = r.get('foo')print(value)

Example output:

$ python test.py
bar
Conclusion
So this is how you can use redis and python to create your database in redis, also you can use for caching for applications. Below is some more resources for you to follow.

Redis with Python
To use Redis with Python, you need a Python Redis client. The following sections demonstrate the use of redis-py, a
docs.redis.com

Redis | The Real-time Data Platform
Build and run faster apps with the world's leading real-time data platform.
redis.com


Original Link: https://dev.to/darkdebo/redis-your-db-for-in-memory-3b0l

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To