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  True if the pool was successfully cleared. False if there was an error.
27
	 *
28
	 * @since   1.0
29
	 */
30
	public function clear()
31
	{
32
		return apc_clear_cache('user');
33
	}
34
35
	/**
36
	 * Returns a Cache Item representing the specified key.
37
	 *
38
	 * @param   string  $key  The key for which to return the corresponding Cache Item.
39
	 *
40
	 * @return  CacheItemInterface  The corresponding Cache Item.
41
	 *
42
	 * @since   __DEPLOY_VERSION__
43
	 */
44
	public function getItem($key)
45
	{
46
		$success = false;
47
		$value = apc_fetch($key, $success);
48
		$item = new Item($key);
49
50
		if ($success)
51
		{
52
			$item->set($value);
53
		}
54
55
		return $item;
56
	}
57
58
	/**
59
	 * Returns a traversable set of cache items.
60
	 *
61
	 * @param   string[]  $keys  An indexed array of keys of items to retrieve.
62
	 *
63
	 * @return  array  A traversable collection of Cache Items keyed by the cache keys of each item.
64
	 *                 A Cache item will be returned for each key, even if that key is not found.
65
	 *
66
	 * @since   __DEPLOY_VERSION__
67
	 */
68
	public function getItems(array $keys = [])
69
	{
70
		$items   = [];
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
	 * Removes the item from the pool.
92
	 *
93
	 * @param   string  $key  The key to delete.
94
	 *
95
	 * @return  boolean  True if the item was successfully removed. False if there was an error.
96
	 *
97
	 * @since   __DEPLOY_VERSION__
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  boolean  True if the item was successfully persisted. False if there was an error.
116
	 *
117
	 * @since   __DEPLOY_VERSION__
118
	 */
119
	public function save(CacheItemInterface $item)
120
	{
121
		// If we are able to find out when the item expires - find out. Else bail.
122
		if ($item instanceof HasExpirationDateInterface)
123
		{
124
			$ttl = $this->convertItemExpiryToSeconds($item);
125
		}
126
		else
127
		{
128
			$ttl = 0;
129
		}
130
131
		return apc_store($item->getKey(), $item->get(), $ttl);
132
	}
133
134
	/**
135
	 * Confirms if the cache contains specified cache item.
136
	 *
137
	 * @param   string  $key  The key for which to check existence.
138
	 *
139
	 * @return  boolean  True if item exists in the cache, false otherwise.
140
	 *
141
	 * @since   1.0
142
	 */
143
	public function hasItem($key)
144
	{
145
		return apc_exists($key);
146
	}
147
148
	/**
149
	 * Test to see if the CacheItemPoolInterface is available
150
	 *
151
	 * @return  boolean  True on success, false otherwise
152
	 *
153
	 * @since   __DEPLOY_VERSION__
154
	 */
155
	public static function isSupported()
156
	{
157
		$supported = extension_loaded('apc') && ini_get('apc.enabled');
158
159
		// If on the CLI interface, the `apc.enable_cli` option must also be enabled
160
		if ($supported && php_sapi_name() === 'cli')
161
		{
162
			$supported = ini_get('apc.enable_cli');
163
		}
164
165
		return (bool) $supported;
166
	}
167
}
168

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  True if the pool was successfully cleared. False if there was an error.
27
	 *
28
	 * @since   __DEPLOY_VERSION__
29
	 */
30
	public function clear()
31
	{
32
		return apcu_clear_cache();
33
	}
34
35
	/**
36
	 * Returns a Cache Item representing the specified key.
37
	 *
38
	 * @param   string  $key  The key for which to return the corresponding Cache Item.
39
	 *
40
	 * @return  CacheItemInterface  The corresponding Cache Item.
41
	 *
42
	 * @since   __DEPLOY_VERSION__
43
	 */
44
	public function getItem($key)
45
	{
46
		$success = false;
47
		$value = apcu_fetch($key, $success);
48
		$item = new Item($key);
49
50
		if ($success)
51
		{
52
			$item->set($value);
53
		}
54
55
		return $item;
56
	}
57
58
	/**
59
	 * Returns a traversable set of cache items.
60
	 *
61
	 * @param   string[]  $keys  An indexed array of keys of items to retrieve.
62
	 *
63
	 * @return  array  A traversable collection of Cache Items keyed by the cache keys of each item.
64
	 *                 A Cache item will be returned for each key, even if that key is not found.
65
	 *
66
	 * @since   __DEPLOY_VERSION__
67
	 */
68
	public function getItems(array $keys = [])
69
	{
70
		$items   = [];
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
	 * Removes the item from the pool.
92
	 *
93
	 * @param   string  $key  The key to delete.
94
	 *
95
	 * @return  boolean  True if the item was successfully removed. False if there was an error.
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  boolean  True if the item was successfully persisted. False if there was an error.
116
	 *
117
	 * @since   __DEPLOY_VERSION__
118
	 */
119
	public function save(CacheItemInterface $item)
120
	{
121
		// If we are able to find out when the item expires - find out. Else bail.
122
		if ($item instanceof HasExpirationDateInterface)
123
		{
124
			$ttl = $this->convertItemExpiryToSeconds($item);
125
		}
126
		else
127
		{
128
			$ttl = 0;
129
		}
130
131
		return apcu_store($item->getKey(), $item->get(), $ttl);
132
	}
133
134
	/**
135
	 * Confirms if the cache contains specified cache item.
136
	 *
137
	 * @param   string  $key  The key for which to check existence.
138
	 *
139
	 * @return  boolean  True if item exists in the cache, false otherwise.
140
	 *
141
	 * @since   __DEPLOY_VERSION__
142
	 */
143
	public function hasItem($key)
144
	{
145
		return apcu_exists($key);
146
	}
147
148
	/**
149
	 * Test to see if the CacheItemPoolInterface is available
150
	 *
151
	 * @return  boolean  True on success, false otherwise
152
	 *
153
	 * @since   __DEPLOY_VERSION__
154
	 */
155
	public static function isSupported()
156
	{
157
		$supported = extension_loaded('apcu') && ini_get('apc.enabled');
158
159
		// If on the CLI interface, the `apc.enable_cli` option must also be enabled
160
		if ($supported && php_sapi_name() === 'cli')
161
		{
162
			$supported = ini_get('apc.enable_cli');
163
		}
164
165
		return (bool) $supported;
166
	}
167
}
168