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.
 
 
 
 
 
 

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