cacheresource.apc.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. /**
  3. * APC CacheResource
  4. *
  5. * CacheResource Implementation based on the KeyValueStore API to use
  6. * memcache as the storage resource for Smarty's output caching.
  7. * *
  8. * @package CacheResource-examples
  9. * @author Uwe Tews
  10. */
  11. class Smarty_CacheResource_Apc extends Smarty_CacheResource_KeyValueStore {
  12. public function __construct()
  13. {
  14. // test if APC is present
  15. if(!function_exists('apc_cache_info')) {
  16. throw new Exception('APC Template Caching Error: APC is not installed');
  17. }
  18. }
  19. /**
  20. * Read values for a set of keys from cache
  21. *
  22. * @param array $keys list of keys to fetch
  23. * @return array list of values with the given keys used as indexes
  24. * @return boolean true on success, false on failure
  25. */
  26. protected function read(array $keys)
  27. {
  28. $_res = array();
  29. $res = apc_fetch($keys);
  30. foreach ($res as $k => $v) {
  31. $_res[$k] = $v;
  32. }
  33. return $_res;
  34. }
  35. /**
  36. * Save values for a set of keys to cache
  37. *
  38. * @param array $keys list of values to save
  39. * @param int $expire expiration time
  40. * @return boolean true on success, false on failure
  41. */
  42. protected function write(array $keys, $expire=null)
  43. {
  44. foreach ($keys as $k => $v) {
  45. apc_store($k, $v, $expire);
  46. }
  47. return true;
  48. }
  49. /**
  50. * Remove values from cache
  51. *
  52. * @param array $keys list of keys to delete
  53. * @return boolean true on success, false on failure
  54. */
  55. protected function delete(array $keys)
  56. {
  57. foreach ($keys as $k) {
  58. apc_delete($k);
  59. }
  60. return true;
  61. }
  62. /**
  63. * Remove *all* values from cache
  64. *
  65. * @return boolean true on success, false on failure
  66. */
  67. protected function purge()
  68. {
  69. return apc_clear_cache('user');
  70. }
  71. }