1: <?php
2: /**
3: * QWatcherCache is a watcher based on the CacheProvider class.
4: * By default, it will use the application cache provider. You can do something different by overriding
5: * initCache in project/includes/controls/QWatcher.class.php.
6: * Note that if you want to be able to detect changes by other users, you should use a
7: * shared caching mechanism, like APC or Memcache. QCacheProviderLocalMemory will
8: * not work for this.
9: */
10:
11: include ('QWatcherBase.class.php');
12:
13: class QWatcherCache extends QWatcherBase {
14:
15: /** @var QAbstractCacheProvider */
16: public static $objCache = null;
17:
18: /**
19: *
20: */
21: public function __construct () {
22: if (!static::$objCache) {
23: static::initCache();
24: }
25: }
26:
27: /**
28: * Records the current state of the watched tables.
29: */
30: public function MakeCurrent() {
31: if (!static::$objCache) {
32: static::initCache();
33: }
34: $curTime = microtime();
35: foreach ($this->strWatchedKeys as $key=>$val) {
36: $time2 = static::$objCache->Get($key);
37:
38: if ($time2===false) {
39: // if dropped from cache, or not yet cached
40: static::$objCache->Set($key, $curTime);
41: $time2 = $curTime;
42: }
43: $this->strWatchedKeys[$key] = $time2;
44: }
45: }
46:
47: /**
48: *
49: * @return bool
50: */
51: public function IsCurrent() {
52: if (!static::$objCache) {
53: static::initCache();
54: }
55: foreach ($this->strWatchedKeys as $key=>$time) {
56: $time2 = static::$objCache->Get($key);
57: if (false===$time2 || $time2 != $time) {
58: return false;
59: }
60: }
61:
62: return true;
63: }
64:
65: /**
66: * Model Save() method should call this to indicate that a table has changed.
67: *
68: * @param string $strTableName
69: * @throws QCallerException
70: */
71: static public function MarkTableModified ($strDbName, $strTableName) {
72: parent::MarkTableModified($strDbName, $strTableName);
73: if (!static::$objCache) {
74: static::initCache();
75: }
76: $key = static::GetKey ($strDbName, $strTableName);
77: $time = microtime();
78:
79: self::$objCache->Set($key, $time);
80: }
81:
82: /**
83: * Initializes the cache. By default, uses the application cache. Configure that in your config
84: * settings.
85: */
86: static protected function initCache(){
87: static::$objCache = QApplication::$objCacheProvider;
88: }
89:
90: }