xcache.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. /**
  3. * Copyright (c) 2012 Bart Visscher <bartv@thisnet.nl>
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. */
  8. class OC_Cache_XCache {
  9. protected $prefix;
  10. public function __construct($global = false) {
  11. $this->prefix = OC_Util::getInstanceId().'/';
  12. if (!$global) {
  13. $this->prefix .= OC_User::getUser().'/';
  14. }
  15. }
  16. /**
  17. * entries in XCache gets namespaced to prevent collisions between owncloud instances and users
  18. */
  19. protected function getNameSpace() {
  20. return $this->prefix;
  21. }
  22. public function get($key) {
  23. return xcache_get($this->getNamespace().$key);
  24. }
  25. public function set($key, $value, $ttl=0) {
  26. if($ttl>0) {
  27. return xcache_set($this->getNamespace().$key, $value, $ttl);
  28. }else{
  29. return xcache_set($this->getNamespace().$key, $value);
  30. }
  31. }
  32. public function hasKey($key) {
  33. return xcache_isset($this->getNamespace().$key);
  34. }
  35. public function remove($key) {
  36. return xcache_unset($this->getNamespace().$key);
  37. }
  38. public function clear($prefix='') {
  39. if(!function_exists('xcache_unset_by_prefix')) {
  40. function xcache_unset_by_prefix($prefix) {
  41. // Since we can't clear targetted cache, we'll clear all. :(
  42. xcache_clear_cache(XC_TYPE_VAR, 0);
  43. }
  44. }
  45. xcache_unset_by_prefix($this->getNamespace().$prefix);
  46. return true;
  47. }
  48. }