-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
📝 update setup.py and split the code to two file to simplify developm…
…ent process
- Loading branch information
Showing
9 changed files
with
105 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
recursive-include *.py | ||
include setup.cfg | ||
include README.md CHANGELOG.md LICENSE.md | ||
include *.txt | ||
recursive-include redismirror/templates * | ||
include README.md LICENSE | ||
include *.md |
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import click | ||
import sys | ||
import redis | ||
import re | ||
from redisCommand import * | ||
# TO DO: | ||
# 1. Add timer to mirror process | ||
# 2. Add auth option | ||
# 3. Support to split it to multi node | ||
# 4. Support all redis command | ||
# 5. Add number of item to mirror limit | ||
|
||
|
||
@click.command() | ||
@click.option("--host", default="127.0.0.1", help="Destination redis host/IP.") | ||
@click.option("--port", default=6379, help="Destination redis port.") | ||
@click.option("--db", default="0", required=False, help="Destination redis DB.") | ||
@click.option("--auth", required=False, help="Destination redis auth info.") | ||
def main(host, port, db, auth): | ||
r = makeConnection(host, port, db, auth) | ||
getSTDOUT(r) | ||
|
||
|
||
def makeConnection(host, port, db, auth): | ||
"""Function to create redis connection | ||
Note: | ||
If the connection failed, the program will exit 1 | ||
Args: | ||
host (str): redis connection string | ||
port (int): redis port number | ||
db (int): redis database name | ||
auth (str): auth info for redis | ||
Returns: | ||
connection: connection object for redis | ||
""" | ||
|
||
pool = redis.ConnectionPool(host=host, port=port, db=db) | ||
r = redis.StrictRedis(connection_pool=pool) | ||
try: | ||
r.ping() | ||
print("Redis is connected..") | ||
except Exception as e: | ||
print(f"Redis connection error ({e})") | ||
sys.exit(1) | ||
return r | ||
|
||
|
||
def getSTDOUT(connection): | ||
if not sys.stdin.isatty(): | ||
input_stream = sys.stdin | ||
else: | ||
print("There is no stdin, check help for more info. exit 1") | ||
sys.exit(1) | ||
for line in input_stream: | ||
try: | ||
tmpLine = line.split(" ", 5) | ||
keyType = str(tmpLine[3]).strip('"') | ||
if keyType == "set": | ||
setFunc(line, connection) | ||
elif keyType == "hset": | ||
hsetFunc(line, connection) | ||
else: | ||
print(f"key type ({keyType}) if not supported yet.") | ||
except: | ||
print("Skip line | {0} ....".format(line[:100])) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
|
||
def setFunc(rLine, connection): | ||
line = rLine.split(" ", 5) | ||
keyName = line[4].strip('"') | ||
keyValue = line[5].strip('"') | ||
try: | ||
connection.set(keyName, keyValue) | ||
print(f"Key mirroed sucessfully | {keyName}") | ||
except Exception as e: | ||
print(f"Failed to set key | {keyName} | {e}") | ||
|
||
|
||
def hsetFunc(rLine, connection): | ||
line = rLine.split(" ", 6) | ||
keyName = line[4].strip('"') | ||
hkeyName = line[5].strip('"') | ||
keyValue = str(line[6].strip('"')).lstrip('"\n') | ||
try: | ||
connection.hset(keyName, hkeyName, keyValue) | ||
print(f"Key mirroed sucessfully | {keyName}") | ||
except Exception as e: | ||
print(f"Failed to set key | {keyName} | {hkeyName} | {e} ") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[metadata] | ||
description-file = README.md |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters