addProxy method

Future<void> addProxy(
  1. ProxyModel proxy
)

Adds a proxy to the cache

The proxy is added to the appropriate tier based on its usage statistics

Implementation

Future<void> addProxy(ProxyModel proxy) async {
  final proxyKey = '${proxy.ip}:${proxy.port}';

  // Increment usage count
  _usageStats[proxyKey] = (_usageStats[proxyKey] ?? 0) + 1;

  // Determine which tier to use based on usage count
  final usageCount = _usageStats[proxyKey] ?? 0;
  final tier = _getTierForUsageCount(usageCount);

  // Remove from current tier if it exists
  _removeProxyFromAllTiers(proxy);

  // Add to the appropriate tier
  final cache = _getCacheForTier(tier);
  cache.add(proxy);

  // Ensure the cache doesn't exceed its maximum size
  _enforceCacheSize(tier);

  // Save changes
  await _saveCaches();
  await _saveUsageStats();
}