Code Duplication    Length = 146-146 lines in 2 locations

src/Adapter/Apc.php 1 location

@@ 21-166 (lines=146) @@
18
 *
19
 * @since  1.0
20
 */
21
class Apc extends AbstractCacheItemPool
22
{
23
	/**
24
	 * This will wipe out the entire cache's keys
25
	 *
26
	 * @return  boolean  The result of the clear operation.
27
	 *
28
	 * @since   1.0
29
	 */
30
	public function clear()
31
	{
32
		return apc_clear_cache('user');
33
	}
34
35
	/**
36
	 * Method to get a storage entry value from a key.
37
	 *
38
	 * @param   string  $key  The storage entry identifier.
39
	 *
40
	 * @return  CacheItemInterface
41
	 *
42
	 * @since   1.0
43
	 * @throws  \RuntimeException
44
	 */
45
	public function getItem($key)
46
	{
47
		$success = false;
48
		$value = apc_fetch($key, $success);
49
		$item = new Item($key);
50
51
		if ($success)
52
		{
53
			$item->set($value);
54
		}
55
56
		return $item;
57
	}
58
59
	/**
60
	 * Obtain multiple CacheItems by their unique keys.
61
	 *
62
	 * @param   array  $keys  A list of keys that can obtained in a single operation.
63
	 *
64
	 * @return  array  An associative array of CacheItem objects keyed on the cache key.
65
	 *
66
	 * @since   1.0
67
	 */
68
	public function getItems(array $keys = array())
69
	{
70
		$items = array();
71
		$success = false;
72
		$values = apc_fetch($keys, $success);
73
74
		if ($success && is_array($values))
75
		{
76
			foreach ($keys as $key)
77
			{
78
				$items[$key] = new Item($key);
79
80
				if (isset($values[$key]))
81
				{
82
					$items[$key]->set($values[$key]);
83
				}
84
			}
85
		}
86
87
		return $items;
88
	}
89
90
	/**
91
	 * Method to remove a storage entry for a key.
92
	 *
93
	 * @param   string  $key  The storage entry identifier.
94
	 *
95
	 * @return  boolean
96
	 *
97
	 * @since   1.0
98
	 */
99
	public function deleteItem($key)
100
	{
101
		if ($this->hasItem($key))
102
		{
103
			return apc_delete($key);
104
		}
105
106
		// If the item doesn't exist, no error
107
		return true;
108
	}
109
110
	/**
111
	 * Persists a cache item immediately.
112
	 *
113
	 * @param   CacheItemInterface  $item  The cache item to save.
114
	 *
115
	 * @return static
116
	 *   The invoked object.
117
	 */
118
	public function save(CacheItemInterface $item)
119
	{
120
		// If we are able to find out when the item expires - find out. Else bail.
121
		if ($item instanceof HasExpirationDateInterface)
122
		{
123
			$ttl = $this->convertItemExpiryToSeconds($item);
124
		}
125
		else
126
		{
127
			$ttl = 0;
128
		}
129
130
		return apc_store($item->getKey(), $item->get(), $ttl);
131
	}
132
133
	/**
134
	 * Method to determine whether a storage entry has been set for a key.
135
	 *
136
	 * @param   string  $key  The storage entry identifier.
137
	 *
138
	 * @return  boolean
139
	 *
140
	 * @since   1.0
141
	 */
142
	public function hasItem($key)
143
	{
144
		return apc_exists($key);
145
	}
146
147
	/**
148
	 * Test to see if the CacheItemPoolInterface is available
149
	 *
150
	 * @return  boolean  True on success, false otherwise
151
	 *
152
	 * @since   __DEPLOY_VERSION__
153
	 */
154
	public static function isSupported()
155
	{
156
		$supported = extension_loaded('apc') && ini_get('apc.enabled');
157
158
		// If on the CLI interface, the `apc.enable_cli` option must also be enabled
159
		if ($supported && php_sapi_name() === 'cli')
160
		{
161
			$supported = ini_get('apc.enable_cli');
162
		}
163
164
		return (bool) $supported;
165
	}
166
}
167

src/Adapter/Apcu.php 1 location

@@ 21-166 (lines=146) @@
18
 *
19
 * @since  __DEPLOY_VERSION__
20
 */
21
class Apcu extends AbstractCacheItemPool
22
{
23
	/**
24
	 * This will wipe out the entire cache's keys
25
	 *
26
	 * @return  boolean  The result of the clear operation.
27
	 *
28
	 * @since   __DEPLOY_VERSION__
29
	 */
30
	public function clear()
31
	{
32
		return apcu_clear_cache();
33
	}
34
35
	/**
36
	 * Method to get a storage entry value from a key.
37
	 *
38
	 * @param   string  $key  The storage entry identifier.
39
	 *
40
	 * @return  CacheItemInterface
41
	 *
42
	 * @since   __DEPLOY_VERSION__
43
	 * @throws  \RuntimeException
44
	 */
45
	public function getItem($key)
46
	{
47
		$success = false;
48
		$value = apcu_fetch($key, $success);
49
		$item = new Item($key);
50
51
		if ($success)
52
		{
53
			$item->set($value);
54
		}
55
56
		return $item;
57
	}
58
59
	/**
60
	 * Obtain multiple CacheItems by their unique keys.
61
	 *
62
	 * @param   array  $keys  A list of keys that can obtained in a single operation.
63
	 *
64
	 * @return  array  An associative array of CacheItem objects keyed on the cache key.
65
	 *
66
	 * @since   __DEPLOY_VERSION__
67
	 */
68
	public function getItems(array $keys = array())
69
	{
70
		$items = array();
71
		$success = false;
72
		$values = apcu_fetch($keys, $success);
73
74
		if ($success && is_array($values))
75
		{
76
			foreach ($keys as $key)
77
			{
78
				$items[$key] = new Item($key);
79
80
				if (isset($values[$key]))
81
				{
82
					$items[$key]->set($values[$key]);
83
				}
84
			}
85
		}
86
87
		return $items;
88
	}
89
90
	/**
91
	 * Method to remove a storage entry for a key.
92
	 *
93
	 * @param   string  $key  The storage entry identifier.
94
	 *
95
	 * @return  boolean
96
	 *
97
	 * @since   __DEPLOY_VERSION__
98
	 */
99
	public function deleteItem($key)
100
	{
101
		if ($this->hasItem($key))
102
		{
103
			return apcu_delete($key);
104
		}
105
106
		// If the item doesn't exist, no error
107
		return true;
108
	}
109
110
	/**
111
	 * Persists a cache item immediately.
112
	 *
113
	 * @param   CacheItemInterface  $item  The cache item to save.
114
	 *
115
	 * @return static
116
	 *   The invoked object.
117
	 */
118
	public function save(CacheItemInterface $item)
119
	{
120
		// If we are able to find out when the item expires - find out. Else bail.
121
		if ($item instanceof HasExpirationDateInterface)
122
		{
123
			$ttl = $this->convertItemExpiryToSeconds($item);
124
		}
125
		else
126
		{
127
			$ttl = 0;
128
		}
129
130
		return apcu_store($item->getKey(), $item->get(), $ttl);
131
	}
132
133
	/**
134
	 * Method to determine whether a storage entry has been set for a key.
135
	 *
136
	 * @param   string  $key  The storage entry identifier.
137
	 *
138
	 * @return  boolean
139
	 *
140
	 * @since   __DEPLOY_VERSION__
141
	 */
142
	public function hasItem($key)
143
	{
144
		return apcu_exists($key);
145
	}
146
147
	/**
148
	 * Test to see if the CacheItemPoolInterface is available
149
	 *
150
	 * @return  boolean  True on success, false otherwise
151
	 *
152
	 * @since   __DEPLOY_VERSION__
153
	 */
154
	public static function isSupported()
155
	{
156
		$supported = extension_loaded('apcu') && ini_get('apc.enabled');
157
158
		// If on the CLI interface, the `apc.enable_cli` option must also be enabled
159
		if ($supported && php_sapi_name() === 'cli')
160
		{
161
			$supported = ini_get('apc.enable_cli');
162
		}
163
164
		return (bool) $supported;
165
	}
166
}
167