Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

An object pool for Scala

License

NotificationsYou must be signed in to change notification settings

andresilva/scala-pool

Repository files navigation

scala-pool is a Scala library for object pooling. The library provides an API and different poolimplementations that allow:

  • blocking/non-blocking object acquisition
  • object invalidation
  • capping the number of pooled objects
  • creating new objects lazily, as needed
  • health checking
  • time-based pool eviction (idle instances)
  • GC-based pool eviction (soft and weak references)
  • efficient thread-safety

Build StatusMaven CentralCoverageLicenseScaladoc

Installation

scala-pool is currently available for Scala 3, Scala 2.13 and 2.12, the latest version is0.5.0.

To use it in an existing SBT project, add the following dependency to yourbuild.sbt:

libraryDependencies+="io.github.andrebeat"%%"scala-pool"%"0.5.0"

The latest snapshot version is also available:

libraryDependencies+="io.github.andrebeat"%%"scala-pool"%"0.5.1-SNAPSHOT"

It might be necessary to add the Sonatype OSS Snapshot resolver:

resolvers+=Resolver.sonatypeRepo("snapshots")

Currently, the library has no external dependencies apart from the Java and Scala standardlibraries.

Usage

The basic usage of the pool is shown below:

importio.github.andrebeat.pool._// Creating a `Pool[Object]` with a capacity of 2 instancesvalpool=Pool(2, ()=>newObject)// Acquiring a lease on an object from the pool (blocking if none available)vallease= pool.acquire()// Using the leaselease { o=>  println(o)}// The object is returned to the pool at this point

All of the different pool features are exposed in thePool companion objectapply method:

Pool(capacity:Int,// the maximum capacity of the poolfactory: ()=>A,// the function used to create new objects in the poolreferenceType:ReferenceType,// the reference type of objects in the poolmaxIdleTime:Duration,// the maximum amount of the time that objects are allowed//   to idle in the pool before being evictedreset:A=>Unit,// the function used to reset objects in the pool//   (called when leasing an object from the pool)dispose:A=>Unit,// the function used to destroy an object from the poolhealthCheck:A=>Boolean)// the predicate used to test whether an object is//   healthy and should be used, or destroyed otherwise

It is also possible to get a value from a lease and release it (or invalidate) manually.

importio.github.andrebeat.pool._// Creating a `Pool[Object]` with a capacity of 2 instancesvalpool=Pool(2, ()=>newObject)// Getting the value from the leasevalobj= lease.get()// There are currently no objects on the poolpool.size// res0: Int = 0// But its capacity is 2 (objects are created lazily)pool.capacity// res1: Int = 2// There's 1 live objectpool.live// res2: Int = 1// And its currently leasedpool.leased// res3: Int = 1// Releasing our lease back to the poollease.release// Its now in the pool waiting to be reusedpool.size// res4: Int = 1// Closes this pool, properly disposing of each pooled object and// releasing any resources associated with the poolpool.close()

The API is documented in depth in theScaladoc.

License

scala-pool is licensed under theMIT license. SeeLICENSEfor details.

About

An object pool for Scala

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors4

  •  
  •  
  •  
  •  

[8]ページ先頭

©2009-2025 Movatter.jp