1: <?php
2: /**
3: * This class provides cache storage in a PHP session.
4: *
5: * Copyright 2010-2012 Horde LLC (http://www.horde.org/)
6: *
7: * See the enclosed file COPYING for license information (LGPL). If you
8: * did not receive this file, see http://www.horde.org/licenses/lgpl21.
9: *
10: * @author Michael Slusarz <slusarz@horde.org>
11: * @category Horde
12: * @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
13: * @package Cache
14: */
15: class Horde_Cache_Storage_Session extends Horde_Cache_Storage_Base
16: {
17: /**
18: * Pointer to the session entry.
19: *
20: * @var array
21: */
22: protected $_sess;
23:
24: /**
25: * Constructor.
26: *
27: * @param array $params Optional parameters:
28: * <pre>
29: * 'session' - (string) Store session data in this entry.
30: * DEFAULT: 'horde_cache_session'
31: * </pre>
32: */
33: public function __construct(array $params = array())
34: {
35: $params = array_merge(array(
36: 'sess_name' => 'horde_cache_session'
37: ), $params);
38:
39: parent::__construct($params);
40:
41: if (!isset($_SESSION[$this->_params['sess_name']])) {
42: $_SESSION[$this->_params['sess_name']] = array();
43: }
44: $this->_sess = &$_SESSION[$this->_params['sess_name']];
45: }
46:
47: /**
48: */
49: public function get($key, $lifetime = 0)
50: {
51: return $this->exists($key, $lifetime)
52: ? $this->_sess[$key]['d']
53: : false;
54: }
55:
56: /**
57: */
58: public function set($key, $data, $lifetime = 0)
59: {
60: $this->_sess[$key] = array(
61: 'd' => $data,
62: 'l' => $lifetime
63: );
64: }
65:
66: /**
67: */
68: public function exists($key, $lifetime = 0)
69: {
70: if (isset($this->_sess[$key])) {
71: /* 0 means no expire. */
72: if (($lifetime == 0) ||
73: ((time() - $lifetime) <= $this->_sess[$key]['l'])) {
74: return true;
75: }
76:
77: unset($this->_sess[$key]);
78: }
79:
80: return false;
81: }
82:
83: /**
84: */
85: public function expire($key)
86: {
87: if (isset($this->_sess[$key])) {
88: unset($this->_sess[$key]);
89: return true;
90: }
91:
92: return false;
93: }
94:
95: /**
96: */
97: public function clear()
98: {
99: $this->_sess = array();
100: }
101:
102: }
103: