Posted By

JulienLeloup on 10/11/13


Tagged

memcached


Versions (?)

Insert key/value pair in memcached


 / Published in: Python
 

Script called in CLI to insert a key/value pair in a local memcached (default installation, on port 11211).

Example : python -k key -v value

Note : this script will show some help with '-h' parameter.

  1. import sys
  2. import argparse
  3. import memcache
  4.  
  5. def main(argv):
  6.  
  7. # Get the key/value pair to be inserted in command line parameters
  8. parser = argparse.ArgumentParser()
  9. parser.add_argument('-k', '--key', required=True, metavar='string', help='Key to be used in memcached')
  10. parser.add_argument('-v', '--value', required=True, metavar='string', help='Value to be inserted in memcached (associated to the key)')
  11. args = parser.parse_args()
  12.  
  13. print "Inserting pair %r -> %r in local memcached" % (args.key, args.value)
  14. print "Warning : this will overide any existing pair with the same key (if there is one) !"
  15.  
  16. # Insert key/value pair in the local memcached
  17. mc = memcache.Client(['127.0.0.1:11211'], debug=0)
  18.  
  19. mc.set(args.key, args.value)
  20.  
  21. if __name__ == "__main__":
  22. main(sys.argv)

Report this snippet  

You need to login to post a comment.