B4J Code Snippet [web]Lettuce.io (working with Redis)

UPDATE: https://www.b4x.com/android/forum/threads/lettuce-v2-0.144169/

B4X:
'Non-UI application (console / server application)
#Region Project Attributes
    #CommandLineArgs:
    #MergeLibraries: True
#End Region
#AdditionalJar: lettuce-core-6.1.8.RELEASE
#AdditionalJar: reactor-core-3.4.19
#AdditionalJar: reactive-streams-1.0.4
#AdditionalJar: netty-common-4.1.78.Final
#AdditionalJar: netty-buffer-4.1.78.Final
#AdditionalJar: netty-codec-4.1.78.Final
#AdditionalJar: netty-handler-4.1.78.Final
#AdditionalJar: netty-resolver-4.1.78.Final
#AdditionalJar: netty-transport-4.1.78.Final
#AdditionalJar: netty-transport-native-unix-common-4.1.78.Final

Sub Process_Globals
 
End Sub

Sub AppStart (Args() As String)
    Dim redis As Lettuce
    redis.Initialize
    redis.Path = "redis://localhost:6379/0"
    redis.WriteMap(CreateMap("key1": "Hello B4J", "key2": "12.5"))
End Sub

Lettuce.bas:
Sub Class_Globals
    Private jo As JavaObject
    Private URL As String
End Sub

Public Sub Initialize
    jo.initializeStatic("io.lettuce.core.RedisClient")
End Sub

Public Sub setPath (mURL As String)
    URL = mURL
End Sub

Public Sub WriteMap (Map As Map)
    Dim redisClient As JavaObject = jo.RunMethod("create", Array(URL))
    Dim connection As JavaObject = redisClient.RunMethod("connect", Null)
    For Each Key As String In Map.Keys
        Dim syncCommands As JavaObject = connection.RunMethod("sync", Null)
        syncCommands.RunMethod("set", Array(Key, Map.Get(Key)))
    Next
    connection.RunMethod("close", Null)
    redisClient.RunMethod("shutdown", Null)
End Sub

Additional Jars download links:
 

Attachments

  • Lettuce.zip
    1.4 KB · Views: 252
Last edited:

aeric

Expert
Licensed User
Longtime User
1656614500020.png


Laragon 5.0.0 is bundled with Redis for Windows (version 3.2.100)

We can use Web Admin or Redis-CLI

1656614672143.png
 

EnriqueGonzalez

Well-Known Member
Licensed User
Longtime User
@aeric i believe that your B4x Career will advance if you start Learning JavaObject
B4X:
dim clientST as javaobject
clientTS.initializeStatic("io.lettuce.core.RedisClient")

dim client as javaobject = clientST.runmethod("create",array("redis://localhost:6379/0"))
dim connection as javaobject = client.runmethod("connect",null)
dim syncCommands as javaobject = connection.runmethod("sync",null)
syncCommands.runmethod("set",array("key","hellob4j"))

        connection.close();
        connection.runmethod("close",null)
        client.runmethod("shutdown",null)
if you wrap each on its own sub you can create a b4j class and use it directly.
(i wrote it in the forum, check for errros)
 

aeric

Expert
Licensed User
Longtime User
@aeric i believe that your B4x Career will advance if you start Learning JavaObject
B4X:
dim clientST as javaobject
clientTS.initializeStatic("io.lettuce.core.RedisClient")

dim client as javaobject = clientST.runmethod("create",array("redis://localhost:6379/0"))
dim connection as javaobject = client.runmethod("connect",null)
dim syncCommands as javaobject = connection.runmethod("sync",null)
syncCommands.runmethod("set",array("key","hellob4j"))

        connection.close();
        connection.runmethod("close",null)
        client.runmethod("shutdown",null)
if you wrap each on its own sub you can create a b4j class and use it directly.
(i wrote it in the forum, check for errros)
Thanks @EnriqueGonzalez , I will try to learn.
 

aeric

Expert
Licensed User
Longtime User
B4X:
Dim redisClient As JavaObject = jo.RunMethod("create", Array("redis://localhost:6379/0"))
In this case, redisClient is JavaObject. Should we use jo.RunMethodJO instead?
I see no difference in if I change.
 

EnriqueGonzalez

Well-Known Member
Licensed User
Longtime User
B4X:
Dim redisClient As JavaObject = jo.RunMethod("create", Array("redis://localhost:6379/0"))
In this case, redisClient is JavaObject. Should we use jo.RunMethodJO instead?
I see no difference in if I change.
runMethodJo is only if you are going to concatenate methods, if not is not needed
 

aeric

Expert
Licensed User
Longtime User
Thank you, what would be the way to obtain the stored data or make the queries?
Please check version 2.
 
Top