Hyperlinkv0.8.0-beta.28

Cache

Cache.sizeconsteffect/Cache.ts:1188
<Key, A, E, R>(self: Cache<Key, A, E, R>): Effect.Effect<number>

Retrieves the approximate number of entries in the cache.

Details

Note that expired entries are counted until they are accessed and removed. The size reflects the current number of entries stored, not the number of valid entries.

Example (Reading cache size)

import { Cache, Effect } from "effect"

const program = Effect.gen(function*() {
  const cache = yield* Cache.make({
    capacity: 10,
    lookup: (key: string) => Effect.succeed(key.length)
  })

  // Empty cache has size 0
  const emptySize = yield* Cache.size(cache)
  console.log(emptySize) // 0

  // Add entries and check size
  yield* Cache.get(cache, "hello")
  yield* Cache.get(cache, "world")
  const sizeAfterAdding = yield* Cache.size(cache)
  console.log(sizeAfterAdding) // 2

  // Size decreases after invalidation
  yield* Cache.invalidate(cache, "hello")
  const sizeAfterInvalidation = yield* Cache.size(cache)
  console.log(sizeAfterInvalidation) // 1
})
combinators
Source effect/Cache.ts:11882 lines
export const size = <Key, A, E, R>(self: Cache<Key, A, E, R>): Effect.Effect<number> =>
  effect.sync(() => MutableHashMap.size(self.map))