-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
762026b
commit 8aa4eb7
Showing
12 changed files
with
485 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package play.api.cache.redis | ||
|
||
import scala.collection.immutable.TreeSet | ||
import scala.language.higherKinds | ||
|
||
trait RedisSortedSet[Elem, Result[_]] extends RedisCollection[TreeSet[Elem], Result] { | ||
override type This = RedisSortedSet[Elem, Result] | ||
|
||
/** | ||
* Adds all the specified members with the specified scores to the sorted set stored at key. | ||
* It is possible to specify multiple score / member pairs. If a specified member is already | ||
* a member of the sorted set, the score is updated and the element reinserted at the right | ||
* position to ensure the correct ordering. | ||
* | ||
* If key does not exist, a new sorted set with the specified members as sole members is created, | ||
* like if the sorted set was empty. | ||
* | ||
* @note If the key exists but does not hold a sorted set, an error is returned. | ||
* @note <strong>Time complexity:</strong> O(log(N)) for each item added, where N is the number of elements in the sorted set. | ||
* @param scoreValues values and corresponding scores to be added | ||
* @return the sorted set for chaining calls | ||
*/ | ||
def add(scoreValues: (Double, Elem)*): Result[This] | ||
|
||
/** | ||
* <p>Tests if the element is contained in the sorted set. Returns true if exists, otherwise returns false</p> | ||
* | ||
* @note <strong>Time complexity:</strong> O(1) | ||
* @param element tested element | ||
* @return true if exists in the set, otherwise false | ||
*/ | ||
def contains(element: Elem): Result[Boolean] | ||
|
||
/** | ||
* <p>Removes the specified members from the sorted set stored at key. Non existing members are ignored. | ||
* An error is returned when key exists and does not hold a sorted set.</p> | ||
* | ||
* @note <strong>Time complexity:</strong> O(M*log(N)) with N being the number of elements in the sorted set and M the number of elements to be removed. | ||
* @param element elements to be removed | ||
* @return the sorted set for chaining calls | ||
*/ | ||
def remove(element: Elem*): Result[This] | ||
|
||
/** | ||
* Returns the specified range of elements in the sorted set stored at key which sorted in order specified by param `isReverse`. | ||
* @param start the start index of the range | ||
* @param stop the stop index of the range | ||
* @param isReverse whether sorted in descending order or not | ||
* @return | ||
*/ | ||
def range(start: Long, stop: Long, isReverse: Boolean = false): Result[Seq[Elem]] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
src/main/scala/play/api/cache/redis/impl/RedisSortedSetImpl.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package play.api.cache.redis.impl | ||
|
||
import play.api.cache.redis._ | ||
|
||
import scala.language.{higherKinds, implicitConversions} | ||
import scala.reflect.ClassTag | ||
|
||
/** <p>Implementation of Set API using redis-server cache implementation.</p> */ | ||
private[impl] class RedisSortedSetImpl[Elem: ClassTag, Result[_]]( | ||
key: String, | ||
redis: RedisConnector | ||
)(implicit | ||
builder: Builders.ResultBuilder[Result], | ||
runtime: RedisRuntime | ||
) extends RedisSortedSet[Elem, Result] { | ||
|
||
// implicit ask timeout and execution context | ||
import dsl._ | ||
|
||
@inline | ||
private def This: This = this | ||
|
||
override def add(scoreValues: (Double, Elem)*): Result[RedisSortedSet[Elem, Result]] = | ||
redis.sortedSetAdd(key, scoreValues: _*).map(_ => This).recoverWithDefault(This) | ||
|
||
override def contains(element: Elem): Result[Boolean] = | ||
redis.sortedSetScore(key, element).map(_.isDefined).recoverWithDefault(false) | ||
|
||
override def remove(element: Elem*): Result[RedisSortedSet[Elem, Result]] = { | ||
redis.sortedSetRemove(key, element: _*).map(_ => This).recoverWithDefault(This) | ||
} | ||
|
||
override def range(start: Long, stop: Long, isReverse: Boolean = false): Result[Seq[Elem]] = { | ||
if (isReverse) { | ||
redis.sortedSetReverseRange[Elem](key, start, stop).recoverWithDefault(Seq.empty) | ||
} else { | ||
redis.sortedSetRange[Elem](key, start, stop).recoverWithDefault(Seq.empty) | ||
} | ||
} | ||
|
||
override def size: Result[Long] = | ||
redis.sortedSetSize(key).recoverWithDefault(0) | ||
|
||
override def isEmpty: Result[Boolean] = | ||
builder.map(size)(_ == 0) | ||
|
||
override def nonEmpty: Result[Boolean] = | ||
builder.map(isEmpty)(x => !x) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.