put method

Future<void> put(
  1. String key,
  2. V value
)

存入缓存数据(写入磁盘)

Implementation

Future<void> put(String key, V value) async {
  final expiresAt = expiration != null
      ? DateTime.now().add(expiration!)
      : null;

  final entry = _CacheEntry(value, expiresAt);

  // 如果 key 已存在,更新访问顺序
  if (_keysIndex.contains(key)) {
    _updateAccessOrder(key);
  } else {
    // 新 key,检查容量
    if (_keysIndex.length >= maxSize) {
      // 淘汰最久未使用的(第一个)
      await _evictOldest();
    }
    _keysIndex.add(key);
  }

  // 写入磁盘
  await _saveToDisk(key, entry);
  await _saveIndex();
}