Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yii2
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
PSDI Army
yii2
Commits
0e2ac865
Commit
0e2ac865
authored
Aug 27, 2013
by
Carsten Brandt
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
added Cache::exists() to check key existance in cache
parent
d34f8486
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
41 additions
and
4 deletions
+41
-4
ApcCache.php
framework/yii/caching/ApcCache.php
+10
-4
Cache.php
framework/yii/caching/Cache.php
+19
-0
CacheTestCase.php
tests/unit/framework/caching/CacheTestCase.php
+12
-0
No files found.
framework/yii/caching/ApcCache.php
View file @
0e2ac865
...
...
@@ -21,12 +21,18 @@ namespace yii\caching;
class
ApcCache
extends
Cache
{
/**
* Checks the existence of a key in APC cache.
* @param string $key the key to be checked.
* @return boolean if the key exists or not
* Checks whether a specified key exists in the cache.
* This can be faster than getting the value from the cache if the data is big.
* Note that this method does not check whether the dependency associated
* with the cached data, if there is any, has changed. So a call to [[get]]
* may return false while exists returns true.
* @param mixed $key a key identifying the cached value. This can be a simple string or
* a complex data structure consisting of factors representing the key.
* @return boolean true if a value exists in cache, false if the value is not in the cache or expired.
*/
public
function
keyE
xists
(
$key
)
public
function
e
xists
(
$key
)
{
$key
=
$this
->
buildKey
(
$key
);
return
apc_exists
(
$key
);
}
...
...
framework/yii/caching/Cache.php
View file @
0e2ac865
...
...
@@ -134,6 +134,25 @@ abstract class Cache extends Component implements \ArrayAccess
}
/**
* Checks whether a specified key exists in the cache.
* This can be faster than getting the value from the cache if the data is big.
* In case a cache does not support this feature natively, this method will try to simulate it
* but has no performance improvement over getting it.
* Note that this method does not check whether the dependency associated
* with the cached data, if there is any, has changed. So a call to [[get]]
* may return false while exists returns true.
* @param mixed $key a key identifying the cached value. This can be a simple string or
* a complex data structure consisting of factors representing the key.
* @return boolean true if a value exists in cache, false if the value is not in the cache or expired.
*/
public
function
exists
(
$key
)
{
$key
=
$this
->
buildKey
(
$key
);
$value
=
$this
->
getValue
(
$key
);
return
$value
!==
false
;
}
/**
* Retrieves multiple values from cache with the specified keys.
* Some caches (such as memcache, apc) allow retrieving multiple cached values at the same time,
* which may improve the performance. In case a cache does not support this feature natively,
...
...
tests/unit/framework/caching/CacheTestCase.php
View file @
0e2ac865
...
...
@@ -91,6 +91,18 @@ abstract class CacheTestCase extends TestCase
$this
->
assertEquals
(
'array_test'
,
$array
[
'array_test'
]);
}
public
function
testExists
()
{
$cache
=
$this
->
prepare
();
$this
->
assertTrue
(
$cache
->
exists
(
'string_test'
));
// check whether exists affects the value
$this
->
assertEquals
(
'string_test'
,
$cache
->
get
(
'string_test'
));
$this
->
assertTrue
(
$cache
->
exists
(
'number_test'
));
$this
->
assertFalse
(
$cache
->
exists
(
'not_exists'
));
}
public
function
testArrayAccess
()
{
$cache
=
$this
->
getCacheInstance
();
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment