B4J Code Snippet [web]Lettuce v2.0

OLDER VERSiON: https://www.b4x.com/android/forum/threads/lettuce-io-working-with-redis.141509/

Main module:
'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.85.Final
#AdditionalJar: netty-buffer-4.1.85.Final
#AdditionalJar: netty-codec-4.1.85.Final
#AdditionalJar: netty-handler-4.1.85.Final
#AdditionalJar: netty-resolver-4.1.85.Final
#AdditionalJar: netty-transport-4.1.85.Final
#AdditionalJar: netty-transport-native-unix-common-4.1.85.Final

Sub Process_Globals
   
End Sub

Sub AppStart (Args() As String)
    Dim value As String
    Dim result As Long
    Dim redis As Lettuce
    redis.Initialize
    redis.Path = "redis://localhost:6379/0"
    redis.Create
    redis.Connect
    redis.WriteMap(CreateMap("key1": "Hello B4J", "key2": "12.25"))
    redis.Set("key3", "36.9")
    redis.Set("key4", "Test")
   
    value = redis.Get("key3")
    Log(value)
   
    value = redis.set("key3", "96.3")
    Log(value)
   
    value = redis.Get("key3")
    Log(value)
   
    result = redis.Del("key3")
    Log(result)

    value = redis.Get("key3")
    Log(value)
   
    redis.Close
    redis.Shutdown
End Sub

Lettuce class:
' Version: 2.0
Sub Class_Globals
    Private jo                 As JavaObject
    Private redisClient     As JavaObject
    Private connection         As JavaObject
    Private syncCommands     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 Create
    redisClient = jo.RunMethod("create", Array(URL))
End Sub

Public Sub Connect
    connection = redisClient.RunMethod("connect", Null)
End Sub


Public Sub Close
    connection.RunMethod("close", Null)
End Sub

Public Sub Shutdown
    redisClient.RunMethod("shutdown", Null)
End Sub

Public Sub Sync
    syncCommands = connection.RunMethod("sync", Null)
End Sub

Public Sub Async
    syncCommands = connection.RunMethod("async", Null)
End Sub

Public Sub WriteMap (Map As Map)
    For Each Key As String In Map.Keys
        Set(Key, Map.Get(Key))
    Next
End Sub

Public Sub Set (Key As String, Value As String)
    Sync
    syncCommands.RunMethod("set", Array(Key, Value))
End Sub

Public Sub Get (Key As String) As String
    Sync
    Return syncCommands.RunMethod("get", Array(Key))
End Sub

Public Sub Del (Key As String) As Long
    Sync
    Return syncCommands.RunMethod("del", Array(Array(Key)))
End Sub
 

Attachments

  • Lettuce.zip
    1.7 KB · Views: 213

alimanam3386

Active Member
Licensed User
Longtime User
Thanks for your job , what about Pub/Sub methods ? it can be implement in your wrap?
 

Inrenbang

Member
Licensed User
Hi, this is what i need, some question tough.
Is it safe to put into websocket?

ws:
'WebSocket class
Sub Class_Globals
    Private ws As WebSocket
    Private redis As Lettuce
End Sub

Public Sub Initialize
    redis.Initialize
    redis.Path = Main.settings.Get("redis_path")
    redis.Create
    redis.Connect
End Sub

Private Sub WebSocket_Connected (WebSocket1 As WebSocket)
    ws = WebSocket1
    redis.Set(ws.Session.Id,ws.Session.Id)
End Sub

Private Sub WebSocket_Disconnected
    redis.Close
    redis.Shutdown
End Sub
 

aeric

Expert
Licensed User
Longtime User
Hi, this is what i need, some question tough.
Is it safe to put into websocket?

ws:
'WebSocket class
Sub Class_Globals
    Private ws As WebSocket
    Private redis As Lettuce
End Sub

Public Sub Initialize
    redis.Initialize
    redis.Path = Main.settings.Get("redis_path")
    redis.Create
    redis.Connect
End Sub

Private Sub WebSocket_Connected (WebSocket1 As WebSocket)
    ws = WebSocket1
    redis.Set(ws.Session.Id,ws.Session.Id)
End Sub

Private Sub WebSocket_Disconnected
    redis.Close
    redis.Shutdown
End Sub
What do you mean by safe?

This library can be use for storing Key-value pairs.

I think you can use:
B4X:
redis.Set("session_id", ws.Session.Id)
 
Top