fileNameFor method

String fileNameFor(
  1. String key
)

Maps a cache key to a filename.

The key is reduced to safe characters plus a hash of the whole key: it may contain slashes, query strings or characters the filesystem rejects, and taking the last path segment (as this once did) collided distinct keys such as posts/1 and comments/1 onto the same file. The readable prefix is kept only to make the cache directory browsable.

Implementation

String fileNameFor(String key) {
  final safe = key
      .replaceAll(RegExp(r'[^A-Za-z0-9._-]'), '_')
      .replaceAll(RegExp(r'_+'), '_');
  final prefix = safe.length <= 40 ? safe : safe.substring(0, 40);
  return '${prefix}_${_hash(key)}.json';
}