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
5d3684fd
Commit
5d3684fd
authored
Jan 13, 2014
by
Paul Klimov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Unit test for MongoDB Cache advanced.
parent
9ca62131
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
110 additions
and
1 deletion
+110
-1
Cache.php
extensions/mongodb/Cache.php
+18
-1
CacheTest.php
tests/unit/extensions/mongodb/CacheTest.php
+92
-0
No files found.
extensions/mongodb/Cache.php
View file @
5d3684fd
...
...
@@ -11,7 +11,23 @@ use Yii;
use
yii\base\InvalidConfigException
;
/**
* Class Cache
* Cache implements a cache application component by storing cached data in a MongoDB.
*
* By default, Cache stores session data in a MongoDB collection named 'cache' inside the default database.
* This collection is better to be pre-created with fields 'id' and 'expire' indexed.
* The table name can be changed by setting [[cacheCollection]].
*
* Please refer to [[\yii\caching\Cache]] for common cache operations that are supported by Cache.
*
* The following example shows how you can configure the application to use Cache:
*
* ~~~
* 'cache' => [
* 'class' => 'yii\mongodb\Cache',
* // 'db' => 'mymongodb',
* // 'cacheCollection' => 'my_cache',
* ]
* ~~~
*
* @author Paul Klimov <klimov.paul@gmail.com>
* @since 2.0
...
...
@@ -26,6 +42,7 @@ class Cache extends \yii\caching\Cache
public
$db
=
'mongodb'
;
/**
* @var string|array the name of the MongoDB collection that stores the cache data.
* This collection is better to be pre-created with fields 'id' and 'expire' indexed.
*/
public
$cacheCollection
=
'cache'
;
/**
...
...
tests/unit/extensions/mongodb/CacheTest.php
View file @
5d3684fd
...
...
@@ -28,6 +28,7 @@ class CacheTest extends MongoDbTestCase
'class'
=>
Cache
::
className
(),
'db'
=>
$this
->
getConnection
(),
'cacheCollection'
=>
static
::
$cacheCollection
,
'gcProbability'
=>
0
,
]);
}
...
...
@@ -40,5 +41,95 @@ class CacheTest extends MongoDbTestCase
$key
=
'test_key'
;
$value
=
'test_value'
;
$this
->
assertTrue
(
$cache
->
set
(
$key
,
$value
),
'Unable to set value!'
);
$this
->
assertEquals
(
$value
,
$cache
->
get
(
$key
),
'Unable to set value correctly!'
);
$newValue
=
'test_new_value'
;
$this
->
assertTrue
(
$cache
->
set
(
$key
,
$newValue
),
'Unable to update value!'
);
$this
->
assertEquals
(
$newValue
,
$cache
->
get
(
$key
),
'Unable to update value correctly!'
);
}
public
function
testAdd
()
{
$cache
=
$this
->
createCache
();
$key
=
'test_key'
;
$value
=
'test_value'
;
$this
->
assertTrue
(
$cache
->
add
(
$key
,
$value
),
'Unable to add value!'
);
$this
->
assertEquals
(
$value
,
$cache
->
get
(
$key
),
'Unable to add value correctly!'
);
$newValue
=
'test_new_value'
;
$this
->
assertTrue
(
$cache
->
add
(
$key
,
$newValue
),
'Unable to re-add value!'
);
$this
->
assertEquals
(
$value
,
$cache
->
get
(
$key
),
'Original value is lost!'
);
}
/**
* @depends testSet
*/
public
function
testDelete
()
{
$cache
=
$this
->
createCache
();
$key
=
'test_key'
;
$value
=
'test_value'
;
$cache
->
set
(
$key
,
$value
);
$this
->
assertTrue
(
$cache
->
delete
(
$key
),
'Unable to delete key!'
);
$this
->
assertEquals
(
false
,
$cache
->
get
(
$key
),
'Value is not deleted!'
);
}
/**
* @depends testSet
*/
public
function
testFlush
()
{
$cache
=
$this
->
createCache
();
$cache
->
set
(
'key1'
,
'value1'
);
$cache
->
set
(
'key2'
,
'value2'
);
$this
->
assertTrue
(
$cache
->
flush
(),
'Unable to flush cache!'
);
$collection
=
$cache
->
db
->
getCollection
(
$cache
->
cacheCollection
);
$rows
=
$this
->
findAll
(
$collection
);
$this
->
assertCount
(
0
,
$rows
,
'Unable to flush records!'
);
}
/**
* @depends testSet
*/
public
function
testGc
()
{
$cache
=
$this
->
createCache
();
$cache
->
set
(
'key1'
,
'value1'
);
$cache
->
set
(
'key2'
,
'value2'
);
$collection
=
$cache
->
db
->
getCollection
(
$cache
->
cacheCollection
);
list
(
$row
)
=
$this
->
findAll
(
$collection
);
$collection
->
update
([
'_id'
=>
$row
[
'_id'
]],
[
'expire'
=>
time
()
-
10
]);
$cache
->
gc
(
true
);
$rows
=
$this
->
findAll
(
$collection
);
$this
->
assertCount
(
1
,
$rows
,
'Unable to collect garbage!'
);
}
/**
* @depends testSet
*/
public
function
testGetExpired
()
{
$cache
=
$this
->
createCache
();
$key
=
'test_key'
;
$value
=
'test_value'
;
$cache
->
set
(
$key
,
$value
);
$collection
=
$cache
->
db
->
getCollection
(
$cache
->
cacheCollection
);
list
(
$row
)
=
$this
->
findAll
(
$collection
);
$collection
->
update
([
'_id'
=>
$row
[
'_id'
]],
[
'expire'
=>
time
()
-
10
]);
$this
->
assertEquals
(
false
,
$cache
->
get
(
$key
),
'Expired key value returned!'
);
}
}
\ No newline at end of file
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