@@ -3,15 +3,181 @@ package com.github.scoquelin.arugula.commands
3
3
import scala .concurrent .Future
4
4
import scala .concurrent .duration .FiniteDuration
5
5
6
+ import com .github .scoquelin .arugula .commands .RedisBaseAsyncCommands .{InitialCursor , ScanResults }
7
+
8
+ import java .time .Instant
9
+
6
10
/**
7
11
* Asynchronous commands for manipulating/querying Keys
8
12
*
9
13
* @tparam K The key type
10
14
* @tparam V The value type
11
15
*/
12
16
trait RedisKeyAsyncCommands [K , V ] {
17
+
18
+ /**
19
+ * Copy a key to another key
20
+ * @param srcKey The key to copy
21
+ * @param destKey The key to copy to
22
+ * @return True if the key was copied, false otherwise
23
+ */
24
+ def copy (srcKey : K , destKey : K ): Future [Boolean ]
25
+
26
+ /**
27
+ * Copy a key to another key with additional arguments
28
+ * @param srcKey The key to copy
29
+ * @param destKey The key to copy to
30
+ * @param args Additional arguments for the copy operation
31
+ */
32
+ def copy (srcKey : K , destKey : K , args : RedisKeyAsyncCommands .CopyArgs ): Future [Unit ]
33
+
34
+ /**
35
+ * Delete one or more keys
36
+ * @param key The key(s) to delete
37
+ * @return The number of keys that were removed
38
+ */
13
39
def del (key : K * ): Future [Long ]
40
+
41
+ /**
42
+ * Unlink one or more keys. (non-blocking version of DEL)
43
+ * @param key The key(s) to unlink
44
+ * @return The number of keys that were unlinked
45
+ */
46
+ def unlink (key : K * ): Future [Long ]
47
+
48
+ /**
49
+ * Serialize a key
50
+ * @param key The key to serialize
51
+ * @return The serialized value of the key
52
+ */
53
+ def dump (key : K ): Future [Array [Byte ]]
54
+
55
+ /**
56
+ * Determine if a key exists
57
+ * @param key The key to check
58
+ * @return True if the key exists, false otherwise
59
+ */
14
60
def exists (key : K * ): Future [Boolean ]
61
+
62
+ /**
63
+ * Set a key's time to live. The key will be automatically deleted after the timeout.
64
+ * Implementations may round the timeout to the nearest second if necessary
65
+ * but could set a more precise timeout if the underlying Redis client supports it.
66
+ * @param key The key to set the expiration for
67
+ * @param expiresIn The duration until the key expires
68
+ * @return True if the timeout was set, false otherwise
69
+ */
15
70
def expire (key : K , expiresIn : FiniteDuration ): Future [Boolean ]
71
+
72
+ /**
73
+ * Set the expiration for a key as an Instant
74
+ * @param key The key to set the expiration for
75
+ * @param timestamp The point in time when the key should expire
76
+ * @return True if the timeout was set, false otherwise
77
+ */
78
+ def expireAt (key : K , timestamp : Instant ): Future [Boolean ]
79
+
80
+ /**
81
+ * Get the time to live for a key as an Instant
82
+ * @param key The key to get the expiration for
83
+ * @return The time to live as a point in time, or None if the key does not exist or does not have an expiration
84
+ */
85
+ def expireTime (key : K ): Future [Option [Instant ]]
86
+
87
+ /**
88
+ * Find all keys matching the given pattern
89
+ * To match all keys, use "*"
90
+ * @param pattern The pattern to match
91
+ * @return The keys that match the pattern
92
+ */
93
+ def keys (pattern : K ): Future [List [K ]]
94
+
95
+ /**
96
+ * Move a key to a different database
97
+ * @param key The key to move
98
+ * @param db The database to move the key to
99
+ * @return True if the key was moved, false otherwise
100
+ */
101
+ def move (key : K , db : Int ): Future [Boolean ]
102
+
103
+ /**
104
+ * Rename a key
105
+ * @param key The key to rename
106
+ * @param newKey The new name for the key
107
+ */
108
+ def rename (key : K , newKey : K ): Future [Unit ]
109
+
110
+ /**
111
+ * Rename a key, but only if the new key does not already exist
112
+ * @param key The key to rename
113
+ * @param newKey The new name for the key
114
+ * @return True if the key was renamed, false otherwise
115
+ */
116
+ def renameNx (key : K , newKey : K ): Future [Boolean ]
117
+
118
+ /**
119
+ * Restore a key from its serialized form
120
+ * @param key The key to restore
121
+ * @param serializedValue The serialized value of the key
122
+ * @param args Additional arguments for the restore operation
123
+ */
124
+ def restore (key : K , serializedValue : Array [Byte ], args : RedisKeyAsyncCommands .RestoreArgs = RedisKeyAsyncCommands .RestoreArgs ()): Future [Unit ]
125
+
126
+ /**
127
+ * Scan the keyspace
128
+ * @param cursor The cursor to start scanning from
129
+ * @param matchPattern An optional pattern to match keys against
130
+ * @param limit An optional limit on the number of keys to return
131
+ * @return The keys that were scanned
132
+ */
133
+ def scan (cursor : String = InitialCursor , matchPattern : Option [String ] = None , limit : Option [Int ] = None ): Future [ScanResults [List [K ]]]
134
+
135
+ /**
136
+ * Get the time to live for a key.
137
+ * Implementations may return a more precise time to live if the underlying Redis client supports it.
138
+ * Rather than expose the underlying Redis client's API, this method returns a FiniteDuration which can
139
+ * be rounded to the nearest second if necessary.
140
+ * @param key The key to get the expiration for
141
+ * @return The time to live, or None if the key does not exist or does not have an expiration
142
+ */
16
143
def ttl (key : K ): Future [Option [FiniteDuration ]]
144
+
145
+ /**
146
+ * Alters the last access time of a key(s). A key is ignored if it does not exist.
147
+ * @param key The key(s) to touch
148
+ * @return The number of keys that were touched
149
+ */
150
+ def touch (key : K * ): Future [Long ]
151
+
152
+ /**
153
+ * Get the type of a key
154
+ * @param key The key to get the type of
155
+ * @return The type of the key
156
+ */
157
+ def `type` (key : K ): Future [String ]
158
+ }
159
+
160
+ object RedisKeyAsyncCommands {
161
+ case class CopyArgs (replace : Boolean = false , destinationDb : Option [Int ] = None )
162
+
163
+ case class RestoreArgs (
164
+ replace : Boolean = false ,
165
+ idleTime : Option [FiniteDuration ] = None ,
166
+ ttl : Option [FiniteDuration ] = None ,
167
+ absTtl : Option [Instant ] = None ,
168
+ frequency : Option [Long ] = None ,
169
+ ){
170
+ def isEmpty : Boolean = ! replace && idleTime.isEmpty && frequency.isEmpty && ttl.isEmpty && absTtl.isEmpty
171
+ }
17
172
}
173
+
174
+ // Commands to be Implemented:
175
+ // migrate
176
+ // objectEncoding
177
+ // objectFreq
178
+ // objectIdletime
179
+ // objectRefcount
180
+ // randomkey
181
+ // sort
182
+ // sortReadOnly
183
+ // sortStore
0 commit comments