Skip to content

Commit 2692200

Browse files
Merge pull request #388 from Quafadas/weakenConstruction
Add sychronous constructor
2 parents 7e974f2 + b92e139 commit 2692200

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

modules/core/src/main/scala/io/chrisdavenport/mules/MemoryCache.scala

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,27 @@ object MemoryCache {
280280
Resource.make(runExpiration(memoryCache).start)(_.cancel).void
281281
}
282282

283+
/**
284+
* Cache construction is Synchronous
285+
*
286+
* Otherwise a copy paste of {@link #ofSingleImmutableMap() ofSingleImmutableMap}
287+
*
288+
**/
289+
def inSingleImmutableMap[G[_] : Sync, F[_]: Async, K, V](
290+
defaultExpiration: Option[TimeSpec]
291+
): G[MemoryCache[F, K, V]] =
292+
Ref.in[G, F, Map[K, MemoryCacheItem[V]]](Map.empty[K, MemoryCacheItem[V]])
293+
.map(ref => new MemoryCache[F, K, V](
294+
MapRef.fromSingleImmutableMapRef(ref),
295+
{(l: Long) => SingleRef.purgeExpiredEntries[F,K, MemoryCacheItem[V]](ref, isExpired)(l)}.some,
296+
defaultExpiration,
297+
{(_, _) => Concurrent[F].unit},
298+
{(_, _) => Concurrent[F].unit},
299+
{(_: K) => Concurrent[F].unit},
300+
{(_: K) => Concurrent[F].unit}
301+
))
302+
303+
283304
/**
284305
* Create a new cache with a default expiration value for newly added cache items.
285306
*

0 commit comments

Comments
 (0)