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
c1328b66
Commit
c1328b66
authored
Jan 18, 2013
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
working on cache key.
parent
abd3d827
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
35 additions
and
15 deletions
+35
-15
Cache.php
framework/caching/Cache.php
+35
-15
No files found.
framework/caching/Cache.php
View file @
c1328b66
...
@@ -10,6 +10,7 @@
...
@@ -10,6 +10,7 @@
namespace
yii\caching
;
namespace
yii\caching
;
use
yii\base\ApplicationComponent
;
use
yii\base\ApplicationComponent
;
use
yii\base\InvalidCallException
;
/**
/**
* Cache is the base class for cache classes supporting different cache storage implementation.
* Cache is the base class for cache classes supporting different cache storage implementation.
...
@@ -48,12 +49,6 @@ abstract class Cache extends ApplicationComponent implements \ArrayAccess
...
@@ -48,12 +49,6 @@ abstract class Cache extends ApplicationComponent implements \ArrayAccess
*/
*/
public
$keyPrefix
;
public
$keyPrefix
;
/**
/**
* @var boolean whether to hash the cache keys so that they can fit in the underlying cache storage.
* Defaults to true. If you set this to be false, you have to make sure the cache keys are allowed by
* the cache storage.
**/
public
$hashKey
=
true
;
/**
* @var array|boolean the functions used to serialize and unserialize cached data. Defaults to null, meaning
* @var array|boolean the functions used to serialize and unserialize cached data. Defaults to null, meaning
* using the default PHP `serialize()` and `unserialize()` functions. If you want to use some more efficient
* using the default PHP `serialize()` and `unserialize()` functions. If you want to use some more efficient
* serializer (e.g. [igbinary](http://pecl.php.net/package/igbinary)), you may configure this property with
* serializer (e.g. [igbinary](http://pecl.php.net/package/igbinary)), you may configure this property with
...
@@ -77,13 +72,38 @@ abstract class Cache extends ApplicationComponent implements \ArrayAccess
...
@@ -77,13 +72,38 @@ abstract class Cache extends ApplicationComponent implements \ArrayAccess
}
}
/**
/**
* Generates a cache key from a given key.
* Generates a cache key from one or multiple parameters.
* The cache key generated is safe to be used to access cache via methods such as [[get()]], [[set()]].
* For example:
*
* ~~~
* $key = Cache::buildKey($className, $method, $id);
* ~~~
*
* @return string the cache key
*/
public
function
generateKey
(
$id
)
{
$n
=
func_num_args
();
if
(
$n
===
1
&&
is_string
(
$id
)
&&
ctype_alnum
(
$id
)
&&
strlen
(
$id
)
<=
32
)
{
return
$this
->
keyPrefix
.
$id
;
}
elseif
(
$n
<
1
)
{
throw
new
InvalidCallException
(
__METHOD__
.
' requires at least one parameter.'
);
}
else
{
$params
=
func_get_args
();
return
$this
->
keyPrefix
.
md5
(
serialize
(
$params
));
}
}
/**
* Generates a normalized key from a given key.
* The normalized key is obtained by hashing the given key via MD5 and then prefixing it with [[keyPrefix]].
* @param string $key a key identifying a value to be cached
* @param string $key a key identifying a value to be cached
* @return string a key generated from the provided key which ensures the uniqueness across applications
* @return string a key generated from the provided key which ensures the uniqueness across applications
*/
*/
protected
function
generate
Cache
Key
(
$key
)
protected
function
generateKey
(
$key
)
{
{
return
$this
->
hashKey
?
md5
(
$this
->
keyPrefix
.
$key
)
:
$this
->
keyPrefix
.
$key
;
return
$this
->
keyPrefix
.
md5
(
$key
)
;
}
}
/**
/**
...
@@ -94,7 +114,7 @@ abstract class Cache extends ApplicationComponent implements \ArrayAccess
...
@@ -94,7 +114,7 @@ abstract class Cache extends ApplicationComponent implements \ArrayAccess
*/
*/
public
function
get
(
$id
)
public
function
get
(
$id
)
{
{
$value
=
$this
->
getValue
(
$this
->
generate
Cache
Key
(
$id
));
$value
=
$this
->
getValue
(
$this
->
generateKey
(
$id
));
if
(
$value
===
false
||
$this
->
serializer
===
false
)
{
if
(
$value
===
false
||
$this
->
serializer
===
false
)
{
return
$value
;
return
$value
;
}
elseif
(
$this
->
serializer
===
null
)
{
}
elseif
(
$this
->
serializer
===
null
)
{
...
@@ -119,11 +139,11 @@ abstract class Cache extends ApplicationComponent implements \ArrayAccess
...
@@ -119,11 +139,11 @@ abstract class Cache extends ApplicationComponent implements \ArrayAccess
* is returned in terms of (key,value) pairs.
* is returned in terms of (key,value) pairs.
* If a value is not cached or expired, the corresponding array value will be false.
* If a value is not cached or expired, the corresponding array value will be false.
*/
*/
public
function
mget
(
array
$ids
)
public
function
mget
(
$ids
)
{
{
$uids
=
array
();
$uids
=
array
();
foreach
(
$ids
as
$id
)
{
foreach
(
$ids
as
$id
)
{
$uids
[
$id
]
=
$this
->
generate
Cache
Key
(
$id
);
$uids
[
$id
]
=
$this
->
generateKey
(
$id
);
}
}
$values
=
$this
->
getValues
(
$uids
);
$values
=
$this
->
getValues
(
$uids
);
$results
=
array
();
$results
=
array
();
...
@@ -167,7 +187,7 @@ abstract class Cache extends ApplicationComponent implements \ArrayAccess
...
@@ -167,7 +187,7 @@ abstract class Cache extends ApplicationComponent implements \ArrayAccess
}
elseif
(
$this
->
serializer
!==
false
)
{
}
elseif
(
$this
->
serializer
!==
false
)
{
$value
=
call_user_func
(
$this
->
serializer
[
0
],
array
(
$value
,
$dependency
));
$value
=
call_user_func
(
$this
->
serializer
[
0
],
array
(
$value
,
$dependency
));
}
}
return
$this
->
setValue
(
$this
->
generate
Cache
Key
(
$id
),
$value
,
$expire
);
return
$this
->
setValue
(
$this
->
generateKey
(
$id
),
$value
,
$expire
);
}
}
/**
/**
...
@@ -190,7 +210,7 @@ abstract class Cache extends ApplicationComponent implements \ArrayAccess
...
@@ -190,7 +210,7 @@ abstract class Cache extends ApplicationComponent implements \ArrayAccess
}
elseif
(
$this
->
serializer
!==
false
)
{
}
elseif
(
$this
->
serializer
!==
false
)
{
$value
=
call_user_func
(
$this
->
serializer
[
0
],
array
(
$value
,
$dependency
));
$value
=
call_user_func
(
$this
->
serializer
[
0
],
array
(
$value
,
$dependency
));
}
}
return
$this
->
addValue
(
$this
->
generate
Cache
Key
(
$id
),
$value
,
$expire
);
return
$this
->
addValue
(
$this
->
generateKey
(
$id
),
$value
,
$expire
);
}
}
/**
/**
...
@@ -200,7 +220,7 @@ abstract class Cache extends ApplicationComponent implements \ArrayAccess
...
@@ -200,7 +220,7 @@ abstract class Cache extends ApplicationComponent implements \ArrayAccess
*/
*/
public
function
delete
(
$id
)
public
function
delete
(
$id
)
{
{
return
$this
->
deleteValue
(
$this
->
generate
Cache
Key
(
$id
));
return
$this
->
deleteValue
(
$this
->
generateKey
(
$id
));
}
}
/**
/**
...
...
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