Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 
 
 

69 rader
2.3 KiB

  1. # Copyright (c) 2012 Web Notes Technologies Pvt Ltd (http://erpnext.com)
  2. #
  3. # MIT License (MIT)
  4. #
  5. # Permission is hereby granted, free of charge, to any person obtaining a
  6. # copy of this software and associated documentation files (the "Software"),
  7. # to deal in the Software without restriction, including without limitation
  8. # the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. # and/or sell copies of the Software, and to permit persons to whom the
  10. # Software is furnished to do so, subject to the following conditions:
  11. #
  12. # The above copyright notice and this permission notice shall be included in
  13. # all copies or substantial portions of the Software.
  14. #
  15. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  16. # INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  17. # PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
  19. # CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
  20. # OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. #
  22. from __future__ import unicode_literals
  23. import memcache, conf
  24. class MClient(memcache.Client):
  25. """memcache client that will automatically prefix conf.db_name
  26. and maintain a key list"""
  27. def n(self, key):
  28. return (conf.db_name + ":" + key.replace(" ", "_")).encode('utf-8')
  29. def set_value(self, key, val):
  30. self.set(self.n(key), val)
  31. self.add_to_key_list(key)
  32. def get_value(self, key):
  33. return self.get(self.n(key))
  34. def delete_value(self, key):
  35. self.delete(self.n(key))
  36. def add_to_key_list(self, key):
  37. key_list = self.get_value('key_list') or []
  38. if key not in key_list:
  39. key_list.append(key)
  40. self.set(self.n("key_list"), key_list)
  41. def delete_keys(self, startswith=None):
  42. """flush keys from known key_list"""
  43. if not startswith:
  44. for key in self.get_value('key_list'):
  45. self.delete_value(key)
  46. self.delete_value('key_list')
  47. else:
  48. deleted = []
  49. keys = self.get_value('key_list') or []
  50. for key in keys:
  51. if key.startswith(startswith):
  52. self.delete(self.n(key))
  53. deleted.append(key)
  54. for d in deleted:
  55. keys.remove(d)
  56. self.set_value("key_list", keys)
  57. # in any case, delete it explicitly
  58. self.delete(self.n(startswith))