Guest User

Untitled

a guest
Sep 18th, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.50 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. # Copyright (c) 2012, lepture.com
  5. #
  6. # Redistribution and use in source and binary forms, with or without
  7. # modification, are permitted provided that the following conditions
  8. # are met:
  9. #
  10. # * Redistributions of source code must retain the above copyright
  11. # notice, this list of conditions and the following disclaimer.
  12. # * Redistributions in binary form must reproduce the above
  13. # copyright notice, this list of conditions and the following
  14. # disclaimer in the documentation and/or other materials provided
  15. # with the distribution.
  16. # * Neither the name of the author nor the names of its contributors
  17. # may be used to endorse or promote products derived from this
  18. # software without specific prior written permission.
  19. #
  20. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. # HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31.  
  32.  
  33. _app_cache = {}
  34.  
  35.  
  36. class InstanceCache(object):
  37. def clear(self):
  38. global _app_cache
  39.  
  40. for key, value in _app_cache.iteritems():
  41. expiry = value[1]
  42. if expiry and time() > expiry:
  43. del _app_cache[key]
  44.  
  45. def flush_all(self):
  46. global _app_cache
  47.  
  48. _app_cache = {}
  49.  
  50. def set(self, key, value, seconds):
  51. global _app_cache
  52.  
  53. if seconds < 0:
  54. seconds = 0
  55.  
  56. _app_cache[key] = (value, time() + seconds if seconds else 0)
  57.  
  58. def get(self, key):
  59. global _app_cache
  60.  
  61. value = _app_cache.get(key, None)
  62. if value:
  63. expiry = value[1]
  64. if expiry and time() > expiry:
  65. del _app_cache[key]
  66. return None
  67. else:
  68. return value[0]
  69. return None
  70.  
  71. def delete(self, key):
  72. global _app_cache
  73.  
  74. if key in _app_cache:
  75. del _app_cache[key]
  76. return None
Add Comment
Please, Sign In to add comment