-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #56 from crowdproj/wip-23
app-tts: use redis as a cache
- Loading branch information
Showing
16 changed files
with
166 additions
and
20 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,23 @@ | ||
package com.gitlab.sszuev.flashcards.speaker | ||
|
||
import io.lettuce.core.api.sync.RedisCommands | ||
import org.slf4j.LoggerFactory | ||
import java.time.Duration | ||
import java.time.Instant | ||
|
||
private val logger = LoggerFactory.getLogger("com.gitlab.sszuev.flashcards.speaker.GetResourceListeners") | ||
|
||
fun onGetResource(commands: RedisCommands<String, String>) = try { | ||
val res1 = commands.incr("words.count.total") | ||
var res2 = commands.incr("words.count.daily") | ||
val date = commands.get("words.date")?.let { Instant.parse(it) } | ||
val now = Instant.now() | ||
if (date != null && Duration.between(date, now).seconds > 24 * 60 * 60) { | ||
// new day | ||
res2 = commands.del("words.count.daily") | ||
commands.set("words.date", now.toString()) | ||
} | ||
logger.info("Total count $res1, today's count $res2") | ||
} catch (ex: Exception) { | ||
logger.error("Unexpected error on get resource", ex) | ||
} |
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,5 @@ | ||
package com.gitlab.sszuev.flashcards.speaker | ||
|
||
data class RedisConfig( | ||
val url: String = "redis://${TTSServerSettings.redisHost}:${TTSServerSettings.redisPort}", | ||
) |
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,36 @@ | ||
package com.gitlab.sszuev.flashcards.speaker | ||
|
||
import io.lettuce.core.RedisClient | ||
import io.lettuce.core.api.sync.RedisCommands | ||
import io.lettuce.core.codec.ByteArrayCodec | ||
import io.lettuce.core.codec.RedisCodec | ||
import io.lettuce.core.codec.StringCodec | ||
|
||
class RedisConnectionFactory( | ||
connectionUrl: String = "redis://localhost:6379", | ||
) : AutoCloseable { | ||
|
||
private val client by lazy { | ||
RedisClient.create(connectionUrl) | ||
} | ||
private val stringToStringConnection by lazy { | ||
client.connect() | ||
} | ||
private val stringToByteArrayConnection by lazy { | ||
client.connect(RedisCodec.of(StringCodec(), ByteArrayCodec())) | ||
} | ||
|
||
val stringToByteArrayCommands: RedisCommands<String, ByteArray> by lazy { | ||
stringToByteArrayConnection.sync() | ||
} | ||
|
||
val stringToStringCommands: RedisCommands<String, String> by lazy { | ||
stringToStringConnection.sync() | ||
} | ||
|
||
override fun close() { | ||
stringToByteArrayConnection.close() | ||
stringToStringConnection.close() | ||
client.shutdown() | ||
} | ||
} |
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,28 @@ | ||
package com.gitlab.sszuev.flashcards.speaker | ||
|
||
import io.lettuce.core.api.sync.RedisCommands | ||
import org.slf4j.LoggerFactory | ||
|
||
private val logger = LoggerFactory.getLogger(RedisResourceCache::class.java) | ||
|
||
class RedisResourceCache( | ||
private val commands: RedisCommands<String, ByteArray> | ||
) : ResourceCache { | ||
|
||
override fun get(id: String): ByteArray? = try { | ||
commands.get(id) | ||
} catch (ex: Exception) { | ||
logger.error("unexpected error while redis#get", ex) | ||
null | ||
} | ||
|
||
override fun put(id: String, data: ByteArray) { | ||
try { | ||
if (commands.set(id, data) != "OK") { | ||
logger.error("Can't redis#set") | ||
} | ||
} catch (ex: Exception) { | ||
logger.error("unexpected error while redis#put", ex) | ||
} | ||
} | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
port: 4222 | ||
max_payload: 5242880 |
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,9 @@ | ||
maxmemory 100mb | ||
maxmemory-policy allkeys-lru | ||
save 900 1 | ||
appendonly yes | ||
appendfsync everysec | ||
auto-aof-rewrite-percentage 50 | ||
auto-aof-rewrite-min-size 64mb | ||
logfile "" | ||
loglevel notice |
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