commiting uncommited changes on live site
[weblabels.fsf.org.git] / crm.fsf.org / 20131203 / files / sites / all / modules-new / civicrm / CRM / Utils / Cache / SqlGroup.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2015
32 * $Id$
33 *
34 */
35
36 /**
37 * This caching provider stores all cached items as a "group" in the
38 * "civicrm_cache" table. The entire 'group' may be prefetched when
39 * instantiating the cache provider.
40 */
41 class CRM_Utils_Cache_SqlGroup implements CRM_Utils_Cache_Interface {
42
43 /**
44 * The host name of the memcached server.
45 *
46 * @var string
47 */
48 protected $group;
49
50 /**
51 * @var int $componentID The optional component ID (so componenets can share the same name space)
52 */
53 protected $componentID;
54
55 /**
56 * @var array in-memory cache to optimize redundant get()s
57 */
58 protected $frontCache;
59
60 /**
61 * Constructor.
62 *
63 * @param array $config
64 * An array of configuration params.
65 * - group: string
66 * - componentID: int
67 * - prefetch: bool, whether to preemptively read the entire cache group; default: TRUE
68 *
69 * @throws RuntimeException
70 * @return \CRM_Utils_Cache_SqlGroup
71 */
72 public function __construct($config) {
73 if (isset($config['group'])) {
74 $this->group = $config['group'];
75 }
76 else {
77 throw new RuntimeException("Cannot construct SqlGroup cache: missing group");
78 }
79 if (isset($config['componentID'])) {
80 $this->componentID = $config['componentID'];
81 }
82 else {
83 $this->componentID = NULL;
84 }
85 $this->frontCache = array();
86 if (CRM_Utils_Array::value('prefetch', $config, TRUE)) {
87 $this->prefetch();
88 }
89 }
90
91 /**
92 * @param string $key
93 * @param mixed $value
94 */
95 public function set($key, &$value) {
96 CRM_Core_BAO_Cache::setItem($value, $this->group, $key, $this->componentID);
97 $this->frontCache[$key] = $value;
98 }
99
100 /**
101 * @param string $key
102 *
103 * @return mixed
104 */
105 public function get($key) {
106 if (!array_key_exists($key, $this->frontCache)) {
107 $this->frontCache[$key] = CRM_Core_BAO_Cache::getItem($this->group, $key, $this->componentID);
108 }
109 return $this->frontCache[$key];
110 }
111
112 /**
113 * @param $key
114 * @param null $default
115 *
116 * @return mixed
117 */
118 public function getFromFrontCache($key, $default = NULL) {
119 return CRM_Utils_Array::value($key, $this->frontCache, $default);
120 }
121
122 /**
123 * @param string $key
124 */
125 public function delete($key) {
126 CRM_Core_BAO_Cache::deleteGroup($this->group, $key);
127 unset($this->frontCache[$key]);
128 }
129
130 public function flush() {
131 CRM_Core_BAO_Cache::deleteGroup($this->group);
132 $this->frontCache = array();
133 }
134
135 public function prefetch() {
136 $this->frontCache = CRM_Core_BAO_Cache::getItems($this->group, $this->componentID);
137 }
138
139 }