You can solve this by making a simple getter/setter pair for the session to have a single source of "truth" :)
-- Here's a snippet that might get you started (I haven't really tested it):
class MyComponent extends Component {
public static function getLocation(){
if (self::$_cachedLocation !== null) {
return self::$_cachedLocation;
}
if (
CakeSession::check('Location.expires')
&& time() > CakeSession::read('Location.expires')
){
CakeSession::delete('Location');
self::$_cachedLocation = null;
return false; // or get new?
}
else {
$return = CakeSession::read('Location');
self::$_cachedLocation = $return;
return self::$_cachedLocation;
}
}
public static function setLocation($data, $expireInHours) {
$data['expires'] = time() + round($expireInHours * 3600);
if (CakeSession::write('Location', $data)){
self::$_cachedLocation = $data;
return self::$_cachedLocation;
}
}
}
You can use this anywhere by using it as a static method call:
$location = MyComponent::getLocation();
if (!$location) { //... get new location data }
MyComponent::setLocation($locationData, 2); //would expire in 2 hours
By caching the result, you make sure your data doesn't "disappear" mid-request.
You could also put the expiration mechanism inside an app-wide beforeFilter? callback, but it makes more sense to check for it only when you need it, to avoid the small overhead.
Hope it helps.
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP
---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/groups/opt_out.
No comments:
Post a Comment