Skip to content

Commit

Permalink
Merge pull request #10 from dinukaamarasinghe817/main
Browse files Browse the repository at this point in the history
Add caching support for persist Redis
  • Loading branch information
daneshk committed Apr 5, 2024
2 parents 938820a + 0c52c30 commit bdceacc
Show file tree
Hide file tree
Showing 15 changed files with 731 additions and 66 deletions.
4 changes: 2 additions & 2 deletions ballerina/Ballerina.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ distribution = "2201.9.0"
graalvmCompatible = true

[[platform.java17.dependency]]
groupId = "io.ballerina.persist"
artifactId = "persist-redis-native"
groupId = "io.ballerina.lib"
artifactId = "persist.redis-native"
version = "0.1.0"
path = "../native/build/libs/persist.redis-native-0.1.0-SNAPSHOT.jar"

Expand Down
13 changes: 13 additions & 0 deletions ballerina/Dependencies.toml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,18 @@ modules = [
{org = "ballerina", packageName = "lang.regexp", moduleName = "lang.regexp"}
]

[[package]]
org = "ballerina"
name = "lang.runtime"
version = "0.0.0"
scope = "testOnly"
dependencies = [
{org = "ballerina", name = "jballerina.java"}
]
modules = [
{org = "ballerina", packageName = "lang.runtime", moduleName = "lang.runtime"}
]

[[package]]
org = "ballerina"
name = "lang.value"
Expand Down Expand Up @@ -152,6 +164,7 @@ version = "0.1.0"
dependencies = [
{org = "ballerina", name = "jballerina.java"},
{org = "ballerina", name = "lang.regexp"},
{org = "ballerina", name = "lang.runtime"},
{org = "ballerina", name = "log"},
{org = "ballerina", name = "persist"},
{org = "ballerina", name = "test"},
Expand Down
9 changes: 7 additions & 2 deletions ballerina/Module.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,17 +62,22 @@ You need to set a value for the following configuration parameter in the `Config
The following is a sample `Config.toml` file with the Redis data store configuration. This is generated by the `persist generate` command.

```toml
[<packageName>.<moduleName>.redis]
[<packageName>.<moduleName>.connectionConfig]
connection = "redis://localhost:6379"

[<packageName>.<moduleName>.cacheConfig]
maxAge = -1
```

Alternatively, you can provide connection parameters instead of the `URI` as follows.
```toml
[<packageName>.<moduleName>.redis.connection]
[<packageName>.<moduleName>.connectionConfig.connection]
host = "localhost"
port = 6379
```

Currently, the Redis data store only supports global cache configuration at the client level. The `maxAge` parameter in the cache configuration specifies the maximum age in `seconds` for cached objects. A value of `-1` indicates that objects within the cache have no expiry time, persisting indefinitely until manually removed.

Additionally, you can set values for the advanced configuration parameters in the Config.toml file in your project to use the Redis data store. For more information on these parameters, see the [Redis Connector documentation](https://central.ballerina.io/ballerinax/redis/latest#ConnectionConfig).

## Setup guide
Expand Down
9 changes: 7 additions & 2 deletions ballerina/Package.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,17 +62,22 @@ You need to set a value for the following configuration parameter in the `Config
The following is a sample `Config.toml` file with the Redis data store configuration. This is generated by the `persist generate` command.

```toml
[<packageName>.<moduleName>.redis]
[<packageName>.<moduleName>.connectionConfig]
connection = "redis://localhost:6379"

[<packageName>.<moduleName>.cacheConfig]
maxAge = -1
```

Alternatively, you can provide connection parameters instead of the `URI` as follows.
```toml
[<packageName>.<moduleName>.redis.connection]
[<packageName>.<moduleName>.connectionConfig.connection]
host = "localhost"
port = 6379
```

Currently, the Redis data store only supports global cache configuration at the client level. The `maxAge` parameter in the cache configuration specifies the maximum age in `seconds` for cached objects. A value of `-1` indicates that objects within the cache have no expiry time, persisting indefinitely until manually removed.

Additionally, you can set values for the advanced configuration parameters in the Config.toml file in your project to use the Redis data store. For more information on these parameters, see the [Redis Connector documentation](https://central.ballerina.io/ballerinax/redis/latest#ConnectionConfig).

## Setup guide
Expand Down
77 changes: 57 additions & 20 deletions ballerina/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -207,29 +207,64 @@ task startRedisTestDockerContainer(type: Exec) {
}
}

task stopRedisTestDockerContainer() {
doLast {
if (!Os.isFamily(Os.FAMILY_WINDOWS)) {
try {
def stdOut = new ByteArrayOutputStream()
exec {
commandLine 'sh', '-c', "docker stop ballerina-persist-redis"
standardOutput = stdOut
}
} catch (all) {
println("Process can safely ignore stopRedisTestDockerContainer task")
task startRedisCacheTestDockerContainer(type: Exec) {
if (!Os.isFamily(Os.FAMILY_WINDOWS)) {
def standardOutput = new ByteArrayOutputStream()
commandLine 'sh', '-c',
"docker run --rm -d --name ballerina-persist-redis-cache -p 6378:6379 -d ballerina-persist-redis"
def healthCheck = 1;
def counter = 0;
doLast {
checkExecResult(executionResult, 'Error', standardOutput)
while (healthCheck != 0 && counter < 12) {
sleep(5 * 1000)
healthCheck = checkRedisTestDockerContainerStatus("ballerina-persist-redis-cache")
counter = counter + 1;
}
} else {
try {
def stdOut = new ByteArrayOutputStream()
exec {
commandLine 'cmd', '/c', "docker stop ballerina-persist-redis"
standardOutput = stdOut
}
} catch (all) {
println("Process can safely ignore stopRedisTestDockerContainer task")
if (healthCheck != 0) {
throw new GradleException("Docker container 'ballerina-persist-redis-cache' health test exceeded timeout!")
}
}
} else {
def standardOutput = new ByteArrayOutputStream()
commandLine 'cmd', '/c',
"docker run --rm -d --name ballerina-persist-redis-cache -p 6378:6379 -d ballerina-persist-redis"
def healthCheck = 1;
def counter = 0;
doLast {
checkExecResult(executionResult, 'Error', standardOutput)
while (healthCheck != 0 && counter < 12) {
sleep(5 * 1000)
healthCheck = checkRedisTestDockerContainerStatus("ballerina-persist-redis-cache")
counter = counter + 1;
}
if (healthCheck != 0) {
throw new GradleException("Docker container 'ballerina-persist-redis-cache' health test exceeded timeout!")
}
}
}
}

task stopRedisTestDockerContainer() {
def stopDockerContainer = { containerName ->
def command
def osFamily = Os.isFamily(Os.FAMILY_WINDOWS) ? 'cmd' : 'sh'
def option = Os.isFamily(Os.FAMILY_WINDOWS) ? '/c' : '-c'

try {
def stdOut = new ByteArrayOutputStream()
exec {
commandLine osFamily, option, "docker stop $containerName"
standardOutput = stdOut
}
} catch (all) {
println("Process can safely ignore stopping $containerName task")
}
}

doLast {
stopDockerContainer("ballerina-persist-redis")
stopDockerContainer("ballerina-persist-redis-cache")
}
}

Expand All @@ -255,6 +290,7 @@ task pullRedisDependency(type: Exec) {
updateTomlFiles.dependsOn copyStdlibs
pullRedisDependency.dependsOn unpackJballerinaTools
startRedisTestDockerContainer.dependsOn createRedisTestDockerImage
startRedisCacheTestDockerContainer.dependsOn createRedisTestDockerImage

build.dependsOn "generatePomFileForMavenPublication"
build.dependsOn ":${packageName}-native:build"
Expand All @@ -263,3 +299,4 @@ build.finalizedBy stopRedisTestDockerContainer

test.dependsOn ":${packageName}-native:build"
test.dependsOn startRedisTestDockerContainer
test.dependsOn startRedisCacheTestDockerContainer
Loading

0 comments on commit bdceacc

Please sign in to comment.