withAppPrefix static method

CacheConfig withAppPrefix(
  1. CacheConfig config,
  2. String appName
)

config where each Redis store with no prefix gets <app name>_cache:, so two apps on one Redis do not share keys. My Shop gives my_shop_cache:. An empty appName changes nothing, so the dox_cache default stays.

Implementation

static CacheConfig withAppPrefix(CacheConfig config, String appName) {
  String name = appName
      .trim()
      .toLowerCase()
      .replaceAll(RegExp(r'[^a-z0-9]+'), '_')
      .replaceAll(RegExp(r'^_+|_+$'), '');
  if (name.isEmpty) return config;
  Map<String, Map<String, dynamic>> stores = <String, Map<String, dynamic>>{
    for (MapEntry<String, Map<String, dynamic>> store
        in config.stores.entries)
      store.key:
          store.value['driver'] == 'redis' && store.value['prefix'] == null
          ? <String, dynamic>{...store.value, 'prefix': '${name}_cache:'}
          : store.value,
  };
  return CacheConfig(defaultStore: config.defaultStore, stores: stores);
}