Skip to content

Commit

Permalink
fix: Bulk action to add or remove
Browse files Browse the repository at this point in the history
  • Loading branch information
Distractic committed Dec 31, 2023
1 parent 9efcac9 commit d4eae95
Show file tree
Hide file tree
Showing 3 changed files with 196 additions and 775 deletions.
21 changes: 14 additions & 7 deletions src/main/kotlin/com/github/rushyverse/core/data/Friend.kt
Original file line number Diff line number Diff line change
Expand Up @@ -343,8 +343,8 @@ public class FriendCacheService(
): Boolean {
return cacheClient.connect { connection ->
val added = add(connection, uuid, friends, addList)
val removed = remove(connection, uuid, friends, removeList)
added || removed
remove(connection, uuid, friends, removeList)
added
}
}

Expand Down Expand Up @@ -381,7 +381,7 @@ public class FriendCacheService(
friends: Collection<UUID>,
type: Type
): Boolean {
return bulkAction(connection, uuid, friends, type) { key, values ->
return bulkAction(uuid, friends, type) { key, values ->
connection.sadd(key, *values)
}
}
Expand All @@ -400,17 +400,24 @@ public class FriendCacheService(
friends: Collection<UUID>,
type: Type
): Boolean {
return bulkAction(connection, uuid, friends, type) { key, values ->
return bulkAction(uuid, friends, type) { key, values ->
connection.srem(key, *values)
}
}

private suspend fun bulkAction(
connection: RedisCoroutinesCommands<ByteArray, ByteArray>,
/**
* Execute an action of [friends] for the given [type].
* @param uuid UUID of the user.
* @param friends UUIDs of the friends.
* @param type Type of the relationship.
* @param action Action to execute.
* @return True if the action was executed successfully, false otherwise.
*/
private suspend inline fun bulkAction(
uuid: UUID,
friends: Collection<UUID>,
type: Type,
action: suspend (ByteArray, Array<ByteArray>) -> Long?
action: (ByteArray, Array<ByteArray>) -> Long?
): Boolean {
if (friends.isEmpty()) return true

Expand Down
Loading

0 comments on commit d4eae95

Please sign in to comment.