-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataPool.php
More file actions
executable file
·180 lines (164 loc) · 3.61 KB
/
Copy pathDataPool.php
File metadata and controls
executable file
·180 lines (164 loc) · 3.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<?php
/**
*
* -+-----------------------------------
* |PHP5 Framework - 2011
* |Web Site: www.iblue.cc
* |E-mail: mejinke@gmail.com
* |Date: 2011-10-08
* -+-----------------------------------
*
* @desc 数据池,在框架运行时保存各种数据
* @author jingke
*/
class XF_DataPool
{
/**
* 当前实例
* @var DataPool
*/
private static $_instance;
/**
* 数据池
* @var array
*/
private $_data_pools = array();
private function __construct(){}
private function __clone(){}
/**
* 获取当前数据池实例
* @return XF_DataPool
*/
public static function getInstance()
{
if (self::$_instance === null)
self::$_instance = new self();
return self::$_instance;
}
/**
* 添加数据
* @param string $key 键
* @param mixed $value 值
* @return XF_DataPool
*/
public function add($key, $value)
{
$this->_data_pools[$key] = $value;
return $this;
}
/**
* 以列表的方式添加数据
* @param string $key 键
* @param mixed $value 值
* @return XF_DataPool
*/
public function addList($key, $value)
{
$this->_data_pools[$key][] = $value;
return $this;
}
/**
* 添加Hash数据
* @param string $key 键
* @param string $item 项名称
* @param mixed $value 值
* @return XF_DataPool
*/
public function addHash($key, $item, $value)
{
$this->_data_pools[$key][$item] = $value;
return $this;
}
/**
* 以Hash列表的方式添加数据
* @param string $key 键
* @param string $item 项名称
* @param mixed $value 值
* @return XF_DataPool
*/
public function addHashList($key, $item, $value)
{
$this->_data_pools[$key][$item][] = $value;
return $this;
}
/**
* 使一个值一直累计数值
* @param string $key 键名称
* @param number $val 值
*/
public function accumulativeNumber($key, $val)
{
$this->_data_pools[$key] = isset($this->_data_pools[$key]) ? $this->_data_pools[$key] + floatval($val) : floatval($val);
}
/**
* 更新池中的内容
* @param string $key 键
* @param mixed $value 值
* @return XF_DataPool
*/
public function update($key, $value)
{
if (isset($this->_data_pools[$key]))
{
$this->_data_pools[$key] = $value;
return true;
}
return false;
}
/**
* 替换池中指定键内容,不存在KEY则添加
* @param string $key 键
* @param mixed $value 值
* @return XF_DataPool
*/
public function replace($key, $value)
{
return $this->add($key, $value);
return $this;
}
/**
* 删除池中指定键的内容
* @param string $key 键
* @return XF_DataPool
*/
public function remove($key)
{
if (isset($this->_data_pools[$key]))
unset($this->_data_pools[$key]);
return true;
}
/**
* 清空数据池
* @return bool
*/
public function removeAll()
{
$this->_data_pools = array();
return true;
}
/**
* 获取池中指定 的内容
* @param string $key 键
* @param mixed $default 如果不存在KEY将要返回的默认为值,默认为false
* @return mixed
*/
public function get($key, $default = false)
{
if (isset($this->_data_pools[$key]))
return $this->_data_pools[$key];
return $default;
}
/**
* 获Hash取池中指定 的内容
* @param string $key 键
* @param string $item 项名称
* @param mixed $default 默认值
* @return mixed
*/
public function getHash($key, $item, $default = false)
{
if (isset($this->_data_pools[$key][$item]))
return $this->_data_pools[$key][$item];
return $default;
}
}