46 const MAX_MODELS_CACHED_IN_MEMORY = 100;
48 private static $modelIdentifiersToModels = array();
50 public static $cacheType =
'M:';
55 public static function getModel($modelIdentifier)
57 assert(
'is_string($modelIdentifier)');
58 assert(
'$modelIdentifier != ""');
59 if (static::supportsAndAllowsPhpCaching() && isset(static::$modelIdentifiersToModels[$modelIdentifier]))
61 return static::$modelIdentifiersToModels[$modelIdentifier];
63 if (static::supportsAndAllowsMemcacheByModelIdentifier($modelIdentifier))
65 $prefix = static::getCachePrefix($modelIdentifier);
66 $model = static::getCachedValueAndValidateChecksum($prefix . $modelIdentifier);
69 static::$modelIdentifiersToModels[$modelIdentifier] = $model;
82 $modelIdentifier = $model->getModelIdentifier();
83 if (static::supportsAndAllowsPhpCaching())
85 static::$modelIdentifiersToModels[$modelIdentifier] = $model;
86 if (count(static::$modelIdentifiersToModels) > static::MAX_MODELS_CACHED_IN_MEMORY)
88 static::$modelIdentifiersToModels = array_slice(static::$modelIdentifiersToModels,
89 count(static::$modelIdentifiersToModels) -
90 static::MAX_MODELS_CACHED_IN_MEMORY);
93 if (static::supportsAndAllowsMemcacheByModel($model))
95 $prefix = static::getCachePrefix($modelIdentifier);
96 static::cacheValueAndChecksum($prefix . $modelIdentifier, $model);
105 $modelIdentifier = $model->getModelIdentifier();
106 static::forgetModelByIdentifier($modelIdentifier);
109 public static function forgetModelByIdentifier($modelIdentifier)
111 if (static::supportsAndAllowsPhpCaching())
113 unset(static::$modelIdentifiersToModels[$modelIdentifier]);
115 if (static::supportsAndAllowsMemcacheByModelIdentifier($modelIdentifier))
117 $prefix = static::getCachePrefix($modelIdentifier);
118 Yii::app()->cache->delete($prefix . $modelIdentifier);
128 public static function forgetAll($onlyForgetPhpCache =
false)
130 if (static::supportsAndAllowsPhpCaching())
132 static::$modelIdentifiersToModels = array();
134 if (!$onlyForgetPhpCache)
136 static::clearMemcacheCache();
146 assert(
'is_string($modelClassName)');
155 static::$modelIdentifiersToModels = array();
158 protected static function supportsAndAllowsMemcacheByModel(
RedBeanModel $model)
160 $className = get_class($model);
161 return static::supportsAndAllowsMemcacheByModelClassName($className);
164 protected static function supportsAndAllowsMemcacheByModelClassName($modelClassName)
166 return ($modelClassName::allowMemcacheCache() && static::supportsAndAllowsMemcache());
169 protected static function supportsAndAllowsMemcacheByModelIdentifier($modelIndetifier)
171 $className = RedBeanModel::getModelClassNameByIdentifier($modelIndetifier);
172 return static::supportsAndAllowsMemcacheByModelClassName($className);
static forgetModel(RedBeanModel $model)
static getModel($modelIdentifier)
static forgetAll($onlyForgetPhpCache=false)
static cacheModel(RedBeanModel $model)
static forgetAllByModelType($modelClassName)
static forgetAllModelIdentifiersToModels()