@@ -31,23 +31,37 @@ class Google_Cache_File extends Google_Cache_Abstract
3131 private $ path ;
3232 private $ fh ;
3333
34+ /**
35+ * @var Google_Client the current client
36+ */
37+ private $ client ;
38+
3439 public function __construct (Google_Client $ client )
3540 {
36- $ this ->path = $ client ->getClassConfig ($ this , 'directory ' );
41+ $ this ->client = $ client ;
42+ $ this ->path = $ this ->client ->getClassConfig ($ this , 'directory ' );
3743 }
38-
44+
3945 public function get ($ key , $ expiration = false )
4046 {
4147 $ storageFile = $ this ->getCacheFile ($ key );
4248 $ data = false ;
43-
49+
4450 if (!file_exists ($ storageFile )) {
51+ $ this ->client ->getLogger ()->debug (
52+ 'File cache miss ' ,
53+ array ('key ' => $ key , 'file ' => $ storageFile )
54+ );
4555 return false ;
4656 }
4757
4858 if ($ expiration ) {
4959 $ mtime = filemtime ($ storageFile );
5060 if ((time () - $ mtime ) >= $ expiration ) {
61+ $ this ->client ->getLogger ()->debug (
62+ 'File cache miss (expired) ' ,
63+ array ('key ' => $ key , 'file ' => $ storageFile )
64+ );
5165 $ this ->delete ($ key );
5266 return false ;
5367 }
@@ -59,6 +73,11 @@ public function get($key, $expiration = false)
5973 $ this ->unlock ($ storageFile );
6074 }
6175
76+ $ this ->client ->getLogger ()->debug (
77+ 'File cache hit ' ,
78+ array ('key ' => $ key , 'file ' => $ storageFile , 'var ' => $ data )
79+ );
80+
6281 return $ data ;
6382 }
6483
@@ -71,17 +90,36 @@ public function set($key, $value)
7190 $ data = serialize ($ value );
7291 $ result = fwrite ($ this ->fh , $ data );
7392 $ this ->unlock ($ storageFile );
93+
94+ $ this ->client ->getLogger ()->debug (
95+ 'File cache set ' ,
96+ array ('key ' => $ key , 'file ' => $ storageFile , 'var ' => $ value )
97+ );
98+ } else {
99+ $ this ->client ->getLogger ()->notice (
100+ 'File cache set failed ' ,
101+ array ('key ' => $ key , 'file ' => $ storageFile )
102+ );
74103 }
75104 }
76105
77106 public function delete ($ key )
78107 {
79108 $ file = $ this ->getCacheFile ($ key );
80109 if (file_exists ($ file ) && !unlink ($ file )) {
110+ $ this ->client ->getLogger ()->error (
111+ 'File cache delete failed ' ,
112+ array ('key ' => $ key , 'file ' => $ file )
113+ );
81114 throw new Google_Cache_Exception ("Cache file could not be deleted " );
82115 }
116+
117+ $ this ->client ->getLogger ()->debug (
118+ 'File cache delete ' ,
119+ array ('key ' => $ key , 'file ' => $ file )
120+ );
83121 }
84-
122+
85123 private function getWriteableCacheFile ($ file )
86124 {
87125 return $ this ->getCacheFile ($ file , true );
@@ -91,7 +129,7 @@ private function getCacheFile($file, $forWrite = false)
91129 {
92130 return $ this ->getCacheDir ($ file , $ forWrite ) . '/ ' . md5 ($ file );
93131 }
94-
132+
95133 private function getCacheDir ($ file , $ forWrite )
96134 {
97135 // use the first 2 characters of the hash as a directory prefix
@@ -100,26 +138,34 @@ private function getCacheDir($file, $forWrite)
100138 $ storageDir = $ this ->path . '/ ' . substr (md5 ($ file ), 0 , 2 );
101139 if ($ forWrite && ! is_dir ($ storageDir )) {
102140 if (! mkdir ($ storageDir , 0755 , true )) {
141+ $ this ->client ->getLogger ()->error (
142+ 'File cache creation failed ' ,
143+ array ('dir ' => $ storageDir )
144+ );
103145 throw new Google_Cache_Exception ("Could not create storage directory: $ storageDir " );
104146 }
105147 }
106148 return $ storageDir ;
107149 }
108-
150+
109151 private function acquireReadLock ($ storageFile )
110152 {
111153 return $ this ->acquireLock (LOCK_SH , $ storageFile );
112154 }
113-
155+
114156 private function acquireWriteLock ($ storageFile )
115157 {
116158 $ rc = $ this ->acquireLock (LOCK_EX , $ storageFile );
117159 if (!$ rc ) {
160+ $ this ->client ->getLogger ()->notice (
161+ 'File cache write lock failed ' ,
162+ array ('file ' => $ storageFile )
163+ );
118164 $ this ->delete ($ storageFile );
119165 }
120166 return $ rc ;
121167 }
122-
168+
123169 private function acquireLock ($ type , $ storageFile )
124170 {
125171 $ mode = $ type == LOCK_EX ? "w " : "r " ;
@@ -134,7 +180,7 @@ private function acquireLock($type, $storageFile)
134180 }
135181 return true ;
136182 }
137-
183+
138184 public function unlock ($ storageFile )
139185 {
140186 if ($ this ->fh ) {
0 commit comments