Implementation of Redis

Github Task Link (https://github.com/samcaspus/red_implementation)
Task Report (https://docs.google.com/document/d/1elozY9I13iF31C5pHWN0GX16acSuzT_JvxGYcwb6hjY/edit#heading=h.9nvcibv3gama)

Introduction
Data storage and retrieval is essence for any data related work. A working implementation of Redis for storage of key value pairs for data retrieval and storage is a requirement. The essence of a key-value store is the ability to store some data, called a value, inside a key. The value can be retrieved later only if we know the specific key it was stored in. Further, when the application ends, the data isn’t lost. The application also has a delay system in order to avoid race conditions.

Aim
The aim of the assignment is to build a working implementation of Redis such as
GET ( https://redis.io/commands/get )
SET ( https://redis.io/commands/set )
EXPIRE ( https://redis.io/commands/expire )
ZADD ( https://redis.io/commands/zadd )
ZRANK ( https://redis.io/commands/zrank )
ZRANGE ( https://redis.io/commands/zrange )
and more if possible

Language : Python
The choice of language (Python) is based on the following criteria:-
Formulating a working implementation of Redis for storage and retrieval of data from specific keys, using Python results in seamless merger .
Quick implementation.
Confidence in programming skill using Python.
Architecture
The layout of the program is primarily divided into driver, instructions and storage verticals. The program verticals are such that the implementations of similar operations are clubbed together which facilitates support to multiple users.
The program consist of the following:
Redis.py is the main driver program which works as a set up for the redis implementation to work.
Filehandeling.py is a file in the modules section which handles all reading and writing of data into a json file.
Instructions.py is a file which contains all the tasks / logic implementations of each redis function such as get,set,zadd,zrange etc.
ExecutionHandler.py improves the function call separately using eval method.
Demo.json Storage of the data is done in demo.json file
Program / Instructions
Requisites. Tools/Components required are as follows:
Python 3 or greater
Json package of python installed
Execution. In order to execute the program the sequence is as follows:
Open terminal and navigate to the clone location

cd Redis
python redis.py
The program is ready to use.
Implementation
Redis.py (Driver page)
Redis.py is the driver program that focuses on running every command given by a user one by one by sending it to the pages that would handle those instructions.
The driver page is imported with Json. Json is used to store the entire data to a file called demo.json. The page consists of the function run which is invoked by the main to execute.
Run function creates two classes in modules folder:
Instructions
File handling
The ‘read’ function is used to read the json data present in demo.json file of the program.
self.redisData = self.FileHandeling.read_json_data()
A never ending while loop is run which terminates only on a specific condition from the user i.e., ‘exit’. The following code gets invoked leading to termination of the program.
if not self.Instructions.behave(self.instructionResponse):
break
self.instructionResponse is the keyword which helps the program to break (“Error word”)

The input instruction is
usersInstruction = input(“>>> “).split()

The function used to read every time, so that any changes that are required to be reflected, can be known before command execution.
self.redisData = self.FileHandeling.temp_read()

The function used to save the modified data into the json format (demo.json)
self.FileHandeling.temp_save(self.redisData)

Once the program terminates, a final confirmation storage of result is done by function
self.FileHandeling.write_json_data(self.redisData)

Inorder to run the program the following code is executed. An object is created for redis and the run method is used to start the application.

redisObject = Redis()
redisObject.run()

FileHandling.py (I/O Storage page)
This file focuses towards reading / writing of data in the demo.json file. Demo.json file acts as a storage for the key value pairs. FileHandling.py has four methods as follows:
Read_json_data
Write_json_data
Temp_save
Temp_read
Read json data uses python’s inbuilt file handling and reads all the content that is present inside demo.json and gives the program a start.
Write json data is a tear down method which writes all the data back in json format to the demo.json file so that all key value pairs are saved.
Temp save is a method that is used inside the while loop so that every time the user executes a command the current instance of the data is saved for the next instance.
Temp read is another method inside the while loop which is present right after the input taken by the user and it is used so that the user gets an instance at that point in time and the program makes a call on how to process the request.

Instructions.py (Commands for redis)
Commands implemented in this application are as follows:
Append
Del
Exists
Exit
Expire
Get
Getrange
Set
Zadd
Zrange and Zrange withscores
Zrankt
ttl
lpush
rpush
lindex
linsert
rinsert
llen
rpop
lpop
lrange
The explanation for the various commands used is as enumerated below:
APPEND
Append is a method that allows the user to add more content to the string already present as value.

DEL
Del is a method which is used to delete any key value pair.
In the above example, as we created a Key value pair sandeep. lets delete it using the del command (del sandeep)

As we use del to delete the command the data is deleted permanently from the json file.
Exist
Exist is a command which is similar to declaration of a key and no value is assigned to it
Eg) Exist sandeep

Exit
Exit is a command which is used to terminate the program successfully in this process the final invoke is taken place to ensure all data is saved properly.
Eg) exit
Expire
Expire is a command used to give a TTL (Time To Live) for a particular key value pair and after that the presence is lost and the time given is in seconds.
Eg) Expire sandeep 10

Once time is over the content gets deleted from the json file and it will not be accessible.
Get
Get method is used to retrieve data from the document, it effectively returns the value stored in the key
Eg) Get sandeep

Getrange
Getrange is a method by which we can set ‘start’ and ‘end’ and get all values in between in that instance

Zadd, Zrange, Zrange withscores, Zrank

These are used to handle pairs.
Zadd helps to add pairs together as value
Zrange helps to display the values within a range (Keys)
Zrange withscores help to display both keys and values within a range
Zrank tells the index of an element if it exists

Demo.json (Storage file)
This is the file where all the content is stored upon saving. A snapshot of the file after having all data saved is shown below:

Ways to Improve
The efficacy of the program can be improved by syncing it via api for any front end facing application (Website/app) which would also improve the response time.
For future development, Instead of using lots of “if-else” statements, a dictionary (map) implementation would improve the performance from O(n) to O(1) .
The data can be stored in a database such as sqlite or mysql.
References
Redis ( https://redis.io/commands/ )
Date time module ( https://docs.python.org/3/library/datetime.html )
Parser module ( https://dateutil.readthedocs.io/en/stable/parser.html )

原文链接:Implementation of Redis

© 版权声明
THE END
喜欢就支持一下吧
点赞11 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容