Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Q
question2answer
Overview
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
outils
question2answer
Commits
28e67ceb
Commit
28e67ceb
authored
Dec 22, 2017
by
Scott
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Ensure unique prefix for cache keys
parent
618372e6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
56 additions
and
16 deletions
+56
-16
CacheDriver.php
qa-include/Q2A/Storage/CacheDriver.php
+7
-0
CacheFactory.php
qa-include/Q2A/Storage/CacheFactory.php
+1
-0
FileCacheDriver.php
qa-include/Q2A/Storage/FileCacheDriver.php
+27
-10
MemcachedDriver.php
qa-include/Q2A/Storage/MemcachedDriver.php
+19
-4
question.php
qa-include/pages/question.php
+1
-1
qa-db.php
qa-include/qa-db.php
+1
-1
No files found.
qa-include/Q2A/Storage/CacheDriver.php
View file @
28e67ceb
...
@@ -76,6 +76,13 @@ interface Q2A_Storage_CacheDriver
...
@@ -76,6 +76,13 @@ interface Q2A_Storage_CacheDriver
public
function
getError
();
public
function
getError
();
/**
/**
* Get the prefix used for all cache keys.
*
* @return string
*/
public
function
getKeyPrefix
();
/**
* Get current statistics for the cache.
* Get current statistics for the cache.
*
*
* @return array Array of stats: 'files' => number of files, 'size' => total file size in bytes.
* @return array Array of stats: 'files' => number of files, 'size' => total file size in bytes.
...
...
qa-include/Q2A/Storage/CacheFactory.php
View file @
28e67ceb
...
@@ -36,6 +36,7 @@ class Q2A_Storage_CacheFactory
...
@@ -36,6 +36,7 @@ class Q2A_Storage_CacheFactory
if
(
self
::
$cacheDriver
===
null
)
{
if
(
self
::
$cacheDriver
===
null
)
{
$config
=
array
(
$config
=
array
(
'enabled'
=>
(
int
)
qa_opt
(
'caching_enabled'
)
===
1
,
'enabled'
=>
(
int
)
qa_opt
(
'caching_enabled'
)
===
1
,
'keyprefix'
=>
QA_FINAL_MYSQL_DATABASE
.
'.'
.
QA_MYSQL_TABLE_PREFIX
.
'.'
,
'dir'
=>
defined
(
'QA_CACHE_DIRECTORY'
)
?
QA_CACHE_DIRECTORY
:
null
,
'dir'
=>
defined
(
'QA_CACHE_DIRECTORY'
)
?
QA_CACHE_DIRECTORY
:
null
,
);
);
...
...
qa-include/Q2A/Storage/FileCacheDriver.php
View file @
28e67ceb
...
@@ -26,6 +26,7 @@
...
@@ -26,6 +26,7 @@
class
Q2A_Storage_FileCacheDriver
implements
Q2A_Storage_CacheDriver
class
Q2A_Storage_FileCacheDriver
implements
Q2A_Storage_CacheDriver
{
{
private
$enabled
=
false
;
private
$enabled
=
false
;
private
$keyPrefix
=
''
;
private
$error
;
private
$error
;
private
$cacheDir
;
private
$cacheDir
;
...
@@ -39,6 +40,10 @@ class Q2A_Storage_FileCacheDriver implements Q2A_Storage_CacheDriver
...
@@ -39,6 +40,10 @@ class Q2A_Storage_FileCacheDriver implements Q2A_Storage_CacheDriver
return
;
return
;
}
}
if
(
isset
(
$config
[
'keyprefix'
]))
{
$this
->
keyPrefix
=
$config
[
'keyprefix'
];
}
if
(
isset
(
$config
[
'dir'
]))
{
if
(
isset
(
$config
[
'dir'
]))
{
$this
->
cacheDir
=
realpath
(
$config
[
'dir'
]);
$this
->
cacheDir
=
realpath
(
$config
[
'dir'
]);
...
@@ -67,14 +72,15 @@ class Q2A_Storage_FileCacheDriver implements Q2A_Storage_CacheDriver
...
@@ -67,14 +72,15 @@ class Q2A_Storage_FileCacheDriver implements Q2A_Storage_CacheDriver
return
null
;
return
null
;
}
}
$file
=
$this
->
getFilename
(
$key
);
$fullKey
=
$this
->
keyPrefix
.
$key
;
$file
=
$this
->
getFilename
(
$fullKey
);
if
(
is_readable
(
$file
))
{
if
(
is_readable
(
$file
))
{
$lines
=
file
(
$file
,
FILE_IGNORE_NEW_LINES
);
$lines
=
file
(
$file
,
FILE_IGNORE_NEW_LINES
);
$actualKey
=
array_shift
(
$lines
);
$actualKey
=
array_shift
(
$lines
);
// double check this is the correct data
// double check this is the correct data
if
(
$
k
ey
===
$actualKey
)
{
if
(
$
fullK
ey
===
$actualKey
)
{
$expiry
=
array_shift
(
$lines
);
$expiry
=
array_shift
(
$lines
);
if
(
is_numeric
(
$expiry
)
&&
time
()
<
$expiry
)
{
if
(
is_numeric
(
$expiry
)
&&
time
()
<
$expiry
)
{
...
@@ -103,13 +109,14 @@ class Q2A_Storage_FileCacheDriver implements Q2A_Storage_CacheDriver
...
@@ -103,13 +109,14 @@ class Q2A_Storage_FileCacheDriver implements Q2A_Storage_CacheDriver
{
{
$success
=
false
;
$success
=
false
;
$ttl
=
(
int
)
$ttl
;
$ttl
=
(
int
)
$ttl
;
$fullKey
=
$this
->
keyPrefix
.
$key
;
if
(
$this
->
enabled
&&
$ttl
>
0
)
{
if
(
$this
->
enabled
&&
$ttl
>
0
)
{
$encData
=
serialize
(
$data
);
$encData
=
serialize
(
$data
);
$expiry
=
time
()
+
(
$ttl
*
60
);
$expiry
=
time
()
+
(
$ttl
*
60
);
$cache
=
$
k
ey
.
"
\n
"
.
$expiry
.
"
\n
"
.
$encData
;
$cache
=
$
fullK
ey
.
"
\n
"
.
$expiry
.
"
\n
"
.
$encData
;
$file
=
$this
->
getFilename
(
$
k
ey
);
$file
=
$this
->
getFilename
(
$
fullK
ey
);
$dir
=
dirname
(
$file
);
$dir
=
dirname
(
$file
);
if
(
is_dir
(
$dir
)
||
mkdir
(
$dir
,
0777
,
true
))
{
if
(
is_dir
(
$dir
)
||
mkdir
(
$dir
,
0777
,
true
))
{
$success
=
@
file_put_contents
(
$file
,
$cache
)
!==
false
;
$success
=
@
file_put_contents
(
$file
,
$cache
)
!==
false
;
...
@@ -127,10 +134,10 @@ class Q2A_Storage_FileCacheDriver implements Q2A_Storage_CacheDriver
...
@@ -127,10 +134,10 @@ class Q2A_Storage_FileCacheDriver implements Q2A_Storage_CacheDriver
*/
*/
public
function
delete
(
$key
)
public
function
delete
(
$key
)
{
{
if
(
$this
->
enabled
)
{
$fullKey
=
$this
->
keyPrefix
.
$key
;
$file
=
$this
->
getFilename
(
$key
);
$dir
=
dirname
(
$key
);
if
(
$this
->
enabled
)
{
$file
=
$this
->
getFilename
(
$fullKey
);
return
$this
->
deleteFile
(
$file
);
return
$this
->
deleteFile
(
$file
);
}
}
...
@@ -209,6 +216,16 @@ class Q2A_Storage_FileCacheDriver implements Q2A_Storage_CacheDriver
...
@@ -209,6 +216,16 @@ class Q2A_Storage_FileCacheDriver implements Q2A_Storage_CacheDriver
}
}
/**
/**
* Get the prefix used for all cache keys.
*
* @return string
*/
public
function
getKeyPrefix
()
{
return
$this
->
keyPrefix
;
}
/**
* Get current statistics for the cache.
* Get current statistics for the cache.
*
*
* @return array Array of stats: 'files' => number of files, 'size' => total file size in bytes.
* @return array Array of stats: 'files' => number of files, 'size' => total file size in bytes.
...
@@ -255,13 +272,13 @@ class Q2A_Storage_FileCacheDriver implements Q2A_Storage_CacheDriver
...
@@ -255,13 +272,13 @@ class Q2A_Storage_FileCacheDriver implements Q2A_Storage_CacheDriver
/**
/**
* Generates filename for cache key, of the form `1/23/123abc`
* Generates filename for cache key, of the form `1/23/123abc`
* @param string $key The unique cache key.
* @param string $key The unique cache key
(including prefix)
.
*
*
* @return string
* @return string
*/
*/
private
function
getFilename
(
$
k
ey
)
private
function
getFilename
(
$
fullK
ey
)
{
{
$filename
=
sha1
(
$
k
ey
);
$filename
=
sha1
(
$
fullK
ey
);
return
$this
->
cacheDir
.
'/'
.
substr
(
$filename
,
0
,
1
)
.
'/'
.
substr
(
$filename
,
1
,
2
)
.
'/'
.
$filename
;
return
$this
->
cacheDir
.
'/'
.
substr
(
$filename
,
0
,
1
)
.
'/'
.
substr
(
$filename
,
1
,
2
)
.
'/'
.
$filename
;
}
}
}
}
qa-include/Q2A/Storage/MemcachedDriver.php
View file @
28e67ceb
...
@@ -27,6 +27,7 @@ class Q2A_Storage_MemcachedDriver implements Q2A_Storage_CacheDriver
...
@@ -27,6 +27,7 @@ class Q2A_Storage_MemcachedDriver implements Q2A_Storage_CacheDriver
{
{
private
$memcached
;
private
$memcached
;
private
$enabled
=
false
;
private
$enabled
=
false
;
private
$keyPrefix
=
''
;
private
$error
;
private
$error
;
private
$flushed
=
false
;
private
$flushed
=
false
;
...
@@ -45,10 +46,14 @@ class Q2A_Storage_MemcachedDriver implements Q2A_Storage_CacheDriver
...
@@ -45,10 +46,14 @@ class Q2A_Storage_MemcachedDriver implements Q2A_Storage_CacheDriver
return
;
return
;
}
}
if
(
isset
(
$config
[
'keyprefix'
]))
{
$this
->
keyPrefix
=
$config
[
'keyprefix'
];
}
if
(
extension_loaded
(
'memcached'
))
{
if
(
extension_loaded
(
'memcached'
))
{
$this
->
memcached
=
new
Memcached
;
$this
->
memcached
=
new
Memcached
;
$this
->
memcached
->
addServer
(
self
::
HOST
,
self
::
PORT
);
$this
->
memcached
->
addServer
(
self
::
HOST
,
self
::
PORT
);
if
(
$this
->
memcached
->
set
(
'q2a.
test'
,
'TEST'
))
{
if
(
$this
->
memcached
->
set
(
$this
->
keyPrefix
.
'
test'
,
'TEST'
))
{
$this
->
enabled
=
true
;
$this
->
enabled
=
true
;
}
else
{
}
else
{
$this
->
setMemcachedError
();
$this
->
setMemcachedError
();
...
@@ -70,7 +75,7 @@ class Q2A_Storage_MemcachedDriver implements Q2A_Storage_CacheDriver
...
@@ -70,7 +75,7 @@ class Q2A_Storage_MemcachedDriver implements Q2A_Storage_CacheDriver
return
null
;
return
null
;
}
}
$result
=
$this
->
memcached
->
get
(
$key
);
$result
=
$this
->
memcached
->
get
(
$
this
->
keyPrefix
.
$
key
);
if
(
$result
===
false
)
{
if
(
$result
===
false
)
{
$this
->
setMemcachedError
();
$this
->
setMemcachedError
();
...
@@ -96,7 +101,7 @@ class Q2A_Storage_MemcachedDriver implements Q2A_Storage_CacheDriver
...
@@ -96,7 +101,7 @@ class Q2A_Storage_MemcachedDriver implements Q2A_Storage_CacheDriver
$ttl
=
(
int
)
$ttl
;
$ttl
=
(
int
)
$ttl
;
$expiry
=
time
()
+
(
$ttl
*
60
);
$expiry
=
time
()
+
(
$ttl
*
60
);
$success
=
$this
->
memcached
->
set
(
$key
,
$data
,
$expiry
);
$success
=
$this
->
memcached
->
set
(
$
this
->
keyPrefix
.
$
key
,
$data
,
$expiry
);
if
(
!
$success
)
{
if
(
!
$success
)
{
$this
->
setMemcachedError
();
$this
->
setMemcachedError
();
...
@@ -117,7 +122,7 @@ class Q2A_Storage_MemcachedDriver implements Q2A_Storage_CacheDriver
...
@@ -117,7 +122,7 @@ class Q2A_Storage_MemcachedDriver implements Q2A_Storage_CacheDriver
return
false
;
return
false
;
}
}
$success
=
$this
->
memcached
->
delete
(
$key
);
$success
=
$this
->
memcached
->
delete
(
$
this
->
keyPrefix
.
$
key
);
if
(
!
$success
)
{
if
(
!
$success
)
{
$this
->
setMemcachedError
();
$this
->
setMemcachedError
();
...
@@ -170,6 +175,16 @@ class Q2A_Storage_MemcachedDriver implements Q2A_Storage_CacheDriver
...
@@ -170,6 +175,16 @@ class Q2A_Storage_MemcachedDriver implements Q2A_Storage_CacheDriver
}
}
/**
/**
* Get the prefix used for all cache keys.
*
* @return string
*/
public
function
getKeyPrefix
()
{
return
$this
->
keyPrefix
;
}
/**
* Get current statistics for the cache.
* Get current statistics for the cache.
*
*
* @return array Array of stats: 'files' => number of files, 'size' => total file size in bytes.
* @return array Array of stats: 'files' => number of files, 'size' => total file size in bytes.
...
...
qa-include/pages/question.php
View file @
28e67ceb
...
@@ -42,7 +42,7 @@ $pagestate = qa_get_state();
...
@@ -42,7 +42,7 @@ $pagestate = qa_get_state();
// Get information about this question
// Get information about this question
$cacheDriver
=
Q2A_Storage_CacheFactory
::
getCacheDriver
();
$cacheDriver
=
Q2A_Storage_CacheFactory
::
getCacheDriver
();
$cacheKey
=
"q
2a.q
uestion:
$questionid
"
;
$cacheKey
=
"question:
$questionid
"
;
$useCache
=
$userid
===
null
&&
$cacheDriver
->
isEnabled
()
&&
!
qa_is_http_post
()
&&
empty
(
$pagestate
);
$useCache
=
$userid
===
null
&&
$cacheDriver
->
isEnabled
()
&&
!
qa_is_http_post
()
&&
empty
(
$pagestate
);
$saveCache
=
false
;
$saveCache
=
false
;
...
...
qa-include/qa-db.php
View file @
28e67ceb
...
@@ -524,7 +524,7 @@ function qa_db_single_select($selectspec)
...
@@ -524,7 +524,7 @@ function qa_db_single_select($selectspec)
// check for cached results
// check for cached results
if
(
isset
(
$selectspec
[
'caching'
]))
{
if
(
isset
(
$selectspec
[
'caching'
]))
{
$cacheDriver
=
Q2A_Storage_CacheFactory
::
getCacheDriver
();
$cacheDriver
=
Q2A_Storage_CacheFactory
::
getCacheDriver
();
$cacheKey
=
'q
2a.q
uery:'
.
$selectspec
[
'caching'
][
'key'
];
$cacheKey
=
'query:'
.
$selectspec
[
'caching'
][
'key'
];
if
(
$cacheDriver
->
isEnabled
())
{
if
(
$cacheDriver
->
isEnabled
())
{
$queryData
=
$cacheDriver
->
get
(
$cacheKey
);
$queryData
=
$cacheDriver
->
get
(
$cacheKey
);
...
...
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