CRM/Utils add comments
[civicrm-core.git] / CRM / Utils / Cache / SqlGroup.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
06b69b18 4 | CiviCRM version 4.5 |
6a488035 5 +--------------------------------------------------------------------+
06b69b18 6 | Copyright CiviCRM LLC (c) 2004-2014 |
6a488035
TO
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
06b69b18 31 * @copyright CiviCRM LLC (c) 2004-2014
6a488035
TO
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 */
41class 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;
f813f78e 54
6a488035
TO
55 /**
56 * @var array in-memory cache to optimize redundant get()s
57 */
58 protected $frontCache;
59
60 /**
61 * Constructor
62 *
77b97be7 63 * @param array $config an array of configuration params
6a488035
TO
64 * - group: string
65 * - componentID: int
66 * - prefetch: bool, whether to preemptively read the entire cache group; default: TRUE
67 *
77b97be7
EM
68 * @throws RuntimeException
69 * @return \CRM_Utils_Cache_SqlGroup
6a488035
TO
70 */
71 function __construct($config) {
72 if (isset($config['group'])) {
73 $this->group = $config['group'];
74 } else {
75 throw new RuntimeException("Cannot construct SqlGroup cache: missing group");
76 }
77 if (isset($config['componentID'])) {
78 $this->componentID = $config['componentID'];
79 } else {
80 $this->componentID = NULL;
81 }
82 $this->frontCache = array();
83 if (CRM_Utils_Array::value('prefetch', $config, TRUE)) {
84 $this->prefetch();
85 }
86 }
87
5bc392e6
EM
88 /**
89 * @param string $key
90 * @param mixed $value
91 */
6a488035
TO
92 function set($key, &$value) {
93 CRM_Core_BAO_Cache::setItem($value, $this->group, $key, $this->componentID);
94 $this->frontCache[$key] = $value;
95 }
96
5bc392e6
EM
97 /**
98 * @param string $key
99 *
100 * @return mixed
101 */
6a488035
TO
102 function get($key) {
103 if (! array_key_exists($key, $this->frontCache)) {
104 $this->frontCache[$key] = CRM_Core_BAO_Cache::getItem($this->group, $key, $this->componentID);
105 }
106 return $this->frontCache[$key];
107 }
108
5bc392e6
EM
109 /**
110 * @param $key
111 * @param null $default
112 *
113 * @return mixed
114 */
20b015e1
TO
115 function getFromFrontCache($key, $default = NULL) {
116 return CRM_Utils_Array::value($key, $this->frontCache, $default);
117 }
118
5bc392e6
EM
119 /**
120 * @param string $key
121 */
6a488035
TO
122 function delete($key) {
123 CRM_Core_BAO_Cache::deleteGroup($this->group, $key);
124 unset($this->frontCache[$key]);
125 }
126
127 function flush() {
128 CRM_Core_BAO_Cache::deleteGroup($this->group);
129 $this->frontCache = array();
130 }
131
132 function prefetch() {
133 $this->frontCache = CRM_Core_BAO_Cache::getItems($this->group, $this->componentID);
134 }
135}