Skip to content

Commit 68f0d46

Browse files
committed
Merge pull request #259 from khoaofgod/final
FIx Bugs March 24
2 parents 19764da + b7e4d90 commit 68f0d46

File tree

5 files changed

+58
-16
lines changed

5 files changed

+58
-16
lines changed

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "phpFastCache/phpFastCache",
2+
"name": "phpfastcache/phpfastcache",
33
"type" : "library",
44
"description": "PHP Cache Class - Supported Redis, Cookie, Predis, APC, MemCached, WinCache, Files, PDO, Reduce mySQL Call by Caching",
55
"keywords": ["cache","caching","php cache","mysql cache","apc cache","apc","memcache","memcached","wincache","files cache","pdo cache","cache class","redis","predis","cookie"],
@@ -33,7 +33,7 @@
3333
},
3434
"autoload": {
3535
"files": [
36-
"src/autoload.php"
36+
"src/phpFastCache/phpFastCache.php"
3737
]
3838
}
3939
}

examples/files.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@
1919
*/
2020

2121
// Include composer autoloader
22-
require '../vendor/autoload.php';
22+
require '../src/autoload.php';
2323
// OR require_once("../src/autoload.php");
2424

2525
use phpFastCache\CacheManager;
2626

2727
// Setup File Path on your config files
2828
CacheManager::setup(array(
29-
"path" => sys_get_temp_dir(), // or in windows "C:/tmp/"
29+
// "path" => sys_get_temp_dir(), // or in windows "C:/tmp/"
3030
));
3131
// our unique method of caching, faster than traditional caching which shared everywhere on internet like 7-10 times
3232
// reduce high load CPU, reduce I/O from files open

examples/sqlite.php

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,11 @@
2121
use phpFastCache\CacheManager;
2222

2323
// Include composer autoloader
24-
require '../vendor/autoload.php';
24+
require '../src/autoload.php';
2525

26-
$InstanceCache = CacheManager::getInstance('sqlite');
26+
CacheManager::setup("storage","sqlite");
27+
28+
$InstanceCache = CacheManager::getInstance();
2729

2830
/**
2931
* Try to get $products from Caching First
@@ -45,4 +47,29 @@
4547
echo $CachedString;
4648
}
4749

50+
echo '<br /><br /><a href="/">Back to index</a>&nbsp;--&nbsp;<a href="/' . basename(__FILE__) . '">Reload</a><br>';
51+
52+
$InstanceCache = CacheManager::sqlite();
53+
54+
/**
55+
* Try to get $products from Caching First
56+
* product_page is "identity keyword";
57+
*/
58+
$key = "product_page2";
59+
$CachedString = $InstanceCache->get($key);
60+
61+
if (is_null($CachedString)) {
62+
$CachedString = "SQLite Cache --> Cache Enabled --> Well done !";
63+
// Write products to Cache in 10 minutes with same keyword
64+
$InstanceCache->set($key, $CachedString, 600);
65+
66+
echo "FIRST LOAD // WROTE OBJECT TO CACHE // RELOAD THE PAGE AND SEE // ";
67+
echo $CachedString;
68+
69+
} else {
70+
echo "READ FROM CACHE // ";
71+
echo $CachedString;
72+
}
73+
4874
echo '<br /><br /><a href="/">Back to index</a>&nbsp;--&nbsp;<a href="/' . basename(__FILE__) . '">Reload</a>';
75+

src/phpFastCache/CacheManager.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -160,12 +160,14 @@ public static function cleanCachingMethod($instance = null)
160160
*/
161161
protected static function __CleanCachingMethod($instance)
162162
{
163-
$old = self::$instances[ $instance ]->config[ 'cache_method' ];
164-
self::$instances[ $instance ]->config[ 'cache_method' ] = 1;
165-
foreach (self::$memory[ $instance ] as $keyword => $object) {
166-
self::$instances[ $instance ]->set($keyword, $object[ 'value' ], $object[ 'expired_in' ]);
163+
if(is_array(self::$memory[ $instance ]) && !empty(self::$memory[ $instance ])) {
164+
$old = self::$instances[$instance]->config['cache_method'];
165+
self::$instances[$instance]->config['cache_method'] = 1;
166+
foreach (self::$memory[$instance] as $keyword => $object) {
167+
self::$instances[$instance]->set($keyword, $object['value'], $object['expired_in']);
168+
}
169+
self::$instances[$instance]->config['cache_method'] = $old;
170+
self::$memory[$instance] = array();
167171
}
168-
self::$instances[ $instance ]->config[ 'cache_method' ] = $old;
169-
self::$memory[ $instance ] = array();
170172
}
171173
}

src/phpFastCache/Core/phpFastCache.php

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -155,13 +155,16 @@ public static function getAutoClass($config)
155155
*/
156156
public static function getPath($skip_create_path = false, $config)
157157
{
158-
if (!isset($config[ 'path' ]) || $config[ 'path' ] == '') {
158+
$tmp_dir = ini_get('upload_tmp_dir') ? ini_get('upload_tmp_dir') : sys_get_temp_dir();
159159

160+
if (!isset($config[ 'path' ]) || $config[ 'path' ] == '') {
160161
if (self::isPHPModule()) {
161-
$tmp_dir = ini_get('upload_tmp_dir') ? ini_get('upload_tmp_dir') : sys_get_temp_dir();
162162
$path = $tmp_dir;
163163
} else {
164-
$path = isset($_SERVER[ 'DOCUMENT_ROOT' ]) ? rtrim($_SERVER[ 'DOCUMENT_ROOT' ], '/') . '/../' : rtrim(__DIR__, '/') . '/';
164+
$document_root_path = rtrim($_SERVER[ 'DOCUMENT_ROOT' ], '/') . '/../';
165+
$path = isset($_SERVER[ 'DOCUMENT_ROOT' ]) && is_writable($document_root_path)
166+
? $document_root_path
167+
: rtrim(__DIR__, '/') . '/';
165168
}
166169

167170
if (self::$config[ 'path' ] != '') {
@@ -187,7 +190,7 @@ public static function getPath($skip_create_path = false, $config)
187190

188191
$securityKey = self::cleanFileName($securityKey);
189192

190-
$full_path = $path . '/' . $securityKey;
193+
$full_path = rtrim($path,'/') . '/' . $securityKey;
191194
$full_pathx = md5($full_path);
192195

193196

@@ -200,6 +203,16 @@ public static function getPath($skip_create_path = false, $config)
200203
if (!@is_writable($full_path)) {
201204
@chmod($full_path, self::__setChmodAuto($config));
202205
}
206+
if(!@is_writable($full_path)) {
207+
// switch back to tmp dir again if the path is not writeable
208+
$full_path = rtrim($tmp_dir,'/') . '/' . $securityKey;
209+
if (!@file_exists($full_path)) {
210+
@mkdir($full_path, self::__setChmodAuto($config));
211+
}
212+
if (!@is_writable($full_path)) {
213+
@chmod($full_path, self::__setChmodAuto($config));
214+
}
215+
}
203216
if (!@file_exists($full_path) || !@is_writable($full_path)) {
204217
throw new phpFastCacheCoreException('PLEASE CREATE OR CHMOD ' . $full_path . ' - 0777 OR ANY WRITABLE PERMISSION!', 92);
205218
}

0 commit comments

Comments
 (0)