estimateCacheItemSize static method

int estimateCacheItemSize(
  1. dynamic value, {
  2. bool hasExpiry = false,
  3. bool hasSlidingExpiry = false,
  4. bool isCompressed = false,
  5. int? originalSize,
})

Estimates the size of a cache item in bytes.

This method takes into account the value, expiry, and other metadata.

Implementation

static int estimateCacheItemSize(dynamic value, {
  bool hasExpiry = false,
  bool hasSlidingExpiry = false,
  bool isCompressed = false,
  int? originalSize,
}) {
  // Base size for cache item metadata
  int size = 64; // Overhead for CacheItem object

  // Add size for value
  size += estimateSize(value);

  // Add size for expiry (if present)
  if (hasExpiry) {
    size += 8; // DateTime is typically 8 bytes
  }

  // Add size for sliding expiry (if present)
  if (hasSlidingExpiry) {
    size += 8; // Duration is typically 8 bytes
  }

  // For compressed items, use the compressed size
  if (isCompressed && originalSize != null) {
    // Replace the value size with the compressed size
    size = size - estimateSize(value) + (originalSize ~/ 2); // Rough estimate of compression
  }

  return size;
}