~ubuntu-branches/ubuntu/wily/php-horde-cache/wily

« back to all changes in this revision

Viewing changes to Horde_Cache-2.4.2/lib/Horde/Cache/Storage/Session.php

  • Committer: Package Import Robot
  • Author(s): Mathieu Parent
  • Date: 2014-05-17 11:10:04 UTC
  • mfrom: (1.1.11)
  • Revision ID: package-import@ubuntu.com-20140517111004-sivxqdkho9p34xfy
Tags: 2.5.0-1
New upstream version 2.5.0

Show diffs side-by-side

added added

removed removed

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