Guest User

Untitled

a guest
Jun 20th, 2018
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.48 KB | None | 0 0
  1. """
  2. Install the following requirements into your virtual environemnt
  3. `pip install click redis`
  4. Usage:
  5.  
  6. To load data into redis
  7. python redis_dump.py load [filepath]
  8.  
  9. To dump data into redis
  10. python redis_dump.py dump [filepath] --search '*txt'
  11. """
  12.  
  13. import click
  14. import redis
  15. import json
  16. import logging
  17. import os
  18.  
  19.  
  20. @click.command()
  21. @click.argument('action')
  22. @click.argument('filepath')
  23. @click.option('--search', help="Key search patter. eg `*txt`")
  24. def main(action, filepath, search):
  25. r = redis.StrictRedis(host='127.0.0.1', port=6379, db=0) # update your redis settings
  26. cache_timeout = None
  27. if action == 'dump':
  28. out = {}
  29. for key in r.scan_iter(search):
  30. out.update({key: r.get(key)})
  31. if len(out) > 0:
  32. try:
  33. with open(filepath, 'w') as outfile:
  34. json.dump(out, outfile)
  35. print('Dump Successful')
  36. except Exception as e:
  37. print(e)
  38. else:
  39. print("Keys not found")
  40.  
  41. elif action == 'load':
  42. try:
  43. with open(filepath) as f:
  44. data = json.load(f)
  45. for key in data:
  46. r.set(key, data.get(key), cache_timeout)
  47. print('Data loaded into redis successfully')
  48. except Exception as e:
  49. print(e)
  50.  
  51.  
  52. if __name__ == '__main__':
  53. log_fmt = '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
  54. logging.basicConfig(level=logging.INFO, format=log_fmt)
  55. main()
Add Comment
Please, Sign In to add comment