/
CacheServiceRedisImpl.scala
292 lines (259 loc) · 9.54 KB
/
CacheServiceRedisImpl.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/*
* Copyright © 2021 - 2022 Swiss National Data and Service Center for the Humanities and/or DaSCH Service Platform contributors.
* SPDX-License-Identifier: Apache-2.0
*/
package org.knora.webapi.store.cacheservice.impl
import org.knora.webapi.exceptions.ForbiddenException
import org.knora.webapi.messages.admin.responder.projectsmessages.ProjectADM
import org.knora.webapi.messages.admin.responder.projectsmessages.ProjectIdentifierADM
import org.knora.webapi.messages.admin.responder.projectsmessages.ProjectIdentifierType
import org.knora.webapi.messages.admin.responder.usersmessages.UserADM
import org.knora.webapi.messages.admin.responder.usersmessages.UserIdentifierADM
import org.knora.webapi.messages.admin.responder.usersmessages.UserIdentifierType
import org.knora.webapi.messages.store.cacheservicemessages.CacheServiceStatusNOK
import org.knora.webapi.messages.store.cacheservicemessages.CacheServiceStatusOK
import org.knora.webapi.messages.store.cacheservicemessages.CacheServiceStatusResponse
import org.knora.webapi.store.cacheservice.api.CacheService
import org.knora.webapi.store.cacheservice.api.EmptyKey
import org.knora.webapi.store.cacheservice.api.EmptyValue
import org.knora.webapi.store.cacheservice.config.RedisConfig
import org.knora.webapi.store.cacheservice.serialization.CacheSerialization
import redis.clients.jedis.Jedis
import redis.clients.jedis.JedisPool
import redis.clients.jedis.JedisPoolConfig
import zio._
case class CacheServiceRedisImpl(pool: JedisPool) extends CacheService {
/**
* Stores the user under the IRI and additionally the IRI under the keys of
* USERNAME and EMAIL:
*
* IRI -> byte array
* username -> IRI
* email -> IRI
*
* @param user the user to store.
*/
def putUserADM(user: UserADM): Task[Unit] =
for {
bytes <- CacheSerialization.serialize(user)
_ <- putBytesValue(user.id, bytes)
_ <- putStringValue(user.username, user.id)
_ <- putStringValue(user.email, user.id)
} yield ()
/**
* Retrieves the user stored under the identifier (either iri, username,
* or email).
*
* The data is stored under the IRI key.
* Additionally, the USERNAME and EMAIL keys point to the IRI key
*
* @param identifier the user identifier.
*/
def getUserADM(identifier: UserIdentifierADM): Task[Option[UserADM]] =
identifier.hasType match {
case UserIdentifierType.Iri => getUserByIri(identifier.toIri)
case UserIdentifierType.Username => getUserByUsernameOrEmail(identifier.toUsername)
case UserIdentifierType.Email => getUserByUsernameOrEmail(identifier.toEmail)
}
/**
* Retrieves the user stored under the IRI.
*
* @param id the user's IRI.
* @return an optional [[UserADM]].
*/
def getUserByIri(id: String): Task[Option[UserADM]] =
(for {
bytes <- getBytesValue(id).some
user <- CacheSerialization.deserialize[UserADM](bytes).some
} yield user).unsome
/**
* Retrieves the user stored under the username or email.
*
* @param usernameOrEmail of the user.
* @return an optional [[UserADM]].
*/
def getUserByUsernameOrEmail(usernameOrEmail: String): Task[Option[UserADM]] =
(for {
iri <- getStringValue(usernameOrEmail).some
user <- getUserByIri(iri).some
} yield user).unsome
/**
* Stores the project under the IRI and additionally the IRI under the keys
* of SHORTCODE and SHORTNAME:
*
* IRI -> byte array
* shortname -> IRI
* shortcode -> IRI
*
* @param value the stored value
*/
def putProjectADM(project: ProjectADM): Task[Unit] =
for {
bytes <- CacheSerialization.serialize(project)
_ <- putBytesValue(project.id, bytes)
_ <- putStringValue(project.shortcode, project.id)
_ <- putStringValue(project.shortname, project.id)
} yield ()
/**
* Retrieves the project stored under the identifier (either iri, shortname, or shortcode).
*
* @param identifier the project identifier.
*/
def getProjectADM(identifier: ProjectIdentifierADM): Task[Option[ProjectADM]] =
// The data is stored under the IRI key.
// Additionally, the SHORTNAME and SHORTCODE keys point to the IRI key
identifier.hasType match {
case ProjectIdentifierType.IRI => getProjectByIri(identifier.toIri)
case ProjectIdentifierType.SHORTCODE => getProjectByShortcodeOrShortname(identifier.toShortcode)
case ProjectIdentifierType.SHORTNAME => getProjectByShortcodeOrShortname(identifier.toShortname)
}
/**
* Retrieves the project stored under the IRI.
*
* @param id the project's IRI
* @return an optional [[ProjectADM]].
*/
def getProjectByIri(id: String): Task[Option[ProjectADM]] =
(for {
bytes <- getBytesValue(id).some
project <- CacheSerialization.deserialize[ProjectADM](bytes).some
} yield project).unsome
/**
* Retrieves the project stored under a SHORTCODE or SHORTNAME.
*
* @param shortcodeOrShortname of the project.
* @return an optional [[ProjectADM]]
*/
def getProjectByShortcodeOrShortname(shortcodeOrShortname: String): Task[Option[ProjectADM]] =
(for {
iri <- getStringValue(shortcodeOrShortname).some
project <- getProjectByIri(iri).some
} yield project).unsome
/**
* Store string value under key.
*
* @param key the key.
* @param value the value.
*/
def putStringValue(key: String, value: String): Task[Unit] = ZIO.attempt {
if (key.isEmpty)
throw EmptyKey("The key under which the value should be written is empty. Aborting writing to redis.")
if (value.isEmpty)
throw EmptyValue("The string value is empty. Aborting writing to redis.")
val conn: Jedis = pool.getResource
try {
conn.set(key, value)
()
} finally {
conn.close()
}
}.catchAll(ex => ZIO.logError(s"Writing to Redis failed: ${ex.getMessage}"))
/**
* Get value stored under the key as a string.
*
* @param maybeKey the key.
*/
def getStringValue(key: String): Task[Option[String]] = {
// FIXME: make it resource safe, i.e., use Scope and add finalizers for the connection
for {
conn <- ZIO.attempt(pool.getResource)
value <- ZIO.attemptBlocking(conn.get(key))
res <-
if (value == "nil".getBytes) ZIO.succeed(None)
else ZIO.succeed(Some(value))
_ = conn.close()
} yield res
}.catchAll(ex => ZIO.logError(s"Reading string from Redis failed: ${ex.getMessage}") *> ZIO.succeed(None))
/**
* Removes values for the provided keys. Any invalid keys are ignored.
*
* @param keys the keys.
*/
def removeValues(keys: Set[String]): Task[Unit] = ZIO.attemptBlocking {
// del takes a vararg so I nee to convert the set to a swq and then to vararg
val conn: Jedis = pool.getResource
try {
conn.del(keys.toSeq: _*)
()
} finally {
conn.close()
}
}.catchAll(ex => ZIO.logError(s"Removing keys from Redis failed: ${ex.getMessage}"))
/**
* Store byte array value under key.
*
* @param key the key.
* @param value the value.
*/
private def putBytesValue(key: String, value: Array[Byte]): Task[Unit] = ZIO.attemptBlocking {
if (key.isEmpty)
throw EmptyKey("The key under which the value should be written is empty. Aborting writing to redis.")
if (value.isEmpty)
throw EmptyValue("The byte array value is empty. Aborting writing to redis.")
val conn: Jedis = pool.getResource
try {
conn.set(key.getBytes, value)
()
} finally {
conn.close()
}
}.catchAll(ex => ZIO.logError(s"Writing to Redis failed: ${ex.getMessage}"))
/**
* Get value stored under the key as a byte array. If no value is found
* under the key, then a [[None]] is returned..
*
* @param key the key.
*/
private def getBytesValue(key: String): Task[Option[Array[Byte]]] =
// FIXME: make it resource safe, i.e., use Scope and add finalizers for the connection
for {
conn <- ZIO.attempt(pool.getResource).onError(ZIO.logErrorCause(_)).orDie
value <- ZIO.attemptBlocking(conn.get(key.getBytes))
res <-
if (value == "nil".getBytes) ZIO.succeed(None)
else ZIO.succeed(Some(value))
_ = conn.close()
} yield res
/**
* Flushes (removes) all stored content from the Redis store.
*/
def flushDB(requestingUser: UserADM): Task[Unit] = ZIO.attemptBlocking {
if (!requestingUser.isSystemUser) {
throw ForbiddenException("Only the system user is allowed to perform this operation.")
}
val conn: Jedis = pool.getResource
try {
conn.flushDB()
()
} finally {
conn.close()
}
}
.catchAll(ex => ZIO.logError(s"Flushing DB failed: ${ex.getMessage}"))
.tap(_ => ZIO.logDebug("Redis cache flushed."))
/**
* Pings the Redis store to see if it is available.
*/
def ping(): Task[CacheServiceStatusResponse] = ZIO.attemptBlocking {
val conn: Jedis = pool.getResource
try {
conn.ping("test")
CacheServiceStatusOK
} finally {
conn.close()
}
}.catchAll(ex => ZIO.logError(s"Ping failed: ${ex.getMessage}") *> ZIO.succeed(CacheServiceStatusNOK))
}
object CacheServiceRedisImpl {
val layer: ZLayer[RedisConfig, Nothing, CacheService] = {
ZLayer {
for {
config <- ZIO.service[RedisConfig]
pool <- ZIO
.attempt(new JedisPool(new JedisPoolConfig(), config.server, config.port))
.onError(ZIO.logErrorCause(_))
.orDie // the Redis Client Pool
} yield CacheServiceRedisImpl(pool)
}.tap(_ => ZIO.debug(">>> Redis Cache Service Initialized <<<"))
}
}