You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

77 lines
2.5 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. """
  23. Simple Caching:
  24. Stores key-value pairs in database and enables simple caching
  25. CacheItem(key).get() returns the cached value if not expired (else returns null)
  26. CacheItem(key).set(interval = 60000) sets a value to cache, expiring after x seconds
  27. CahceItem(key).clear() clears an old value
  28. setup() sets up cache
  29. """
  30. import webnotes
  31. def clear():
  32. """clear doctype cache"""
  33. try:
  34. webnotes.conn.sql("""delete from __CacheItem""")
  35. except Exception, e:
  36. if e.args[0]!=1146: raise e
  37. class CacheItem:
  38. def __init__(self, key):
  39. """create a new cache"""
  40. self.key = key
  41. def get(self):
  42. """get value"""
  43. try:
  44. return webnotes.conn.sql("""select `value` from __CacheItem where
  45. `key`=%s and ifnull(expires_on, '2100-01-01') > NOW()""", self.key)[0][0]
  46. except Exception:
  47. return None
  48. def set(self, value, interval=None):
  49. """set a new value, with interval"""
  50. self.clear()
  51. if interval:
  52. webnotes.conn.sql("""insert into
  53. __CacheItem (`key`, `value`, expires_on)
  54. values (%s, %s, addtime(now(), sec_to_time(%s)))
  55. """, (self.key, str(value), interval))
  56. else:
  57. webnotes.conn.sql("""insert into
  58. __CacheItem (`key`, `value`)
  59. values (%s, %s)
  60. """, (self.key, str(value)))
  61. def clear(self):
  62. """clear the item"""
  63. try:
  64. webnotes.conn.sql("delete from __CacheItem where `key`=%s", self.key)
  65. except Exception, e:
  66. if e.args[0]!=1146: raise e