Lettuce multiple reactive Redis stores and transactions across stores












0















I'm learning Redis for Java is something that I think I'm really missing about the Redis api.

Say we have the following code for creating a connection:



RedisClient redisClient = RedisClient
.create("redis://password@localhost:6379/");
StatefulRedisConnection<String, String> connection
= redisClient.connect();


This defines a client for key type String and value type String.

Now what do I do when I need to handle multiple Redis objects, like not only String/String, but for example multiple sets with different types?

Should I create a different connection for each?

I have tried to use the reactive templates but get into the same problem i that I would create multiple instances with different types.



When dealing with transactions I don't understand how to use a transaction across a single template.

For example, I want to insert a serialized post into a Redis store like:



Key postId | Value <post> 


But in one transaction, I also want to add the postId in a set that represents a feed:



Key topic | Value <set with post Id's>


All examples that I found perform transactions on the same template, but I have no idea how to continue with this.

Pointers are appreciated.










share|improve this question























  • How about using StatefulRedisConnection <Object, Object>

    – Sid Malani
    Nov 30 '18 at 3:02











  • A working example would be appreciated. I currently solved it by removing the map postId-post as I don’t really need it and store posts directly in the zset. But it’s just a lucky coincidence. I realise Redis is not relational... but somehow I thought it would be capable of this.

    – Trace
    Nov 30 '18 at 9:09
















0















I'm learning Redis for Java is something that I think I'm really missing about the Redis api.

Say we have the following code for creating a connection:



RedisClient redisClient = RedisClient
.create("redis://password@localhost:6379/");
StatefulRedisConnection<String, String> connection
= redisClient.connect();


This defines a client for key type String and value type String.

Now what do I do when I need to handle multiple Redis objects, like not only String/String, but for example multiple sets with different types?

Should I create a different connection for each?

I have tried to use the reactive templates but get into the same problem i that I would create multiple instances with different types.



When dealing with transactions I don't understand how to use a transaction across a single template.

For example, I want to insert a serialized post into a Redis store like:



Key postId | Value <post> 


But in one transaction, I also want to add the postId in a set that represents a feed:



Key topic | Value <set with post Id's>


All examples that I found perform transactions on the same template, but I have no idea how to continue with this.

Pointers are appreciated.










share|improve this question























  • How about using StatefulRedisConnection <Object, Object>

    – Sid Malani
    Nov 30 '18 at 3:02











  • A working example would be appreciated. I currently solved it by removing the map postId-post as I don’t really need it and store posts directly in the zset. But it’s just a lucky coincidence. I realise Redis is not relational... but somehow I thought it would be capable of this.

    – Trace
    Nov 30 '18 at 9:09














0












0








0








I'm learning Redis for Java is something that I think I'm really missing about the Redis api.

Say we have the following code for creating a connection:



RedisClient redisClient = RedisClient
.create("redis://password@localhost:6379/");
StatefulRedisConnection<String, String> connection
= redisClient.connect();


This defines a client for key type String and value type String.

Now what do I do when I need to handle multiple Redis objects, like not only String/String, but for example multiple sets with different types?

Should I create a different connection for each?

I have tried to use the reactive templates but get into the same problem i that I would create multiple instances with different types.



When dealing with transactions I don't understand how to use a transaction across a single template.

For example, I want to insert a serialized post into a Redis store like:



Key postId | Value <post> 


But in one transaction, I also want to add the postId in a set that represents a feed:



Key topic | Value <set with post Id's>


All examples that I found perform transactions on the same template, but I have no idea how to continue with this.

Pointers are appreciated.










share|improve this question














I'm learning Redis for Java is something that I think I'm really missing about the Redis api.

Say we have the following code for creating a connection:



RedisClient redisClient = RedisClient
.create("redis://password@localhost:6379/");
StatefulRedisConnection<String, String> connection
= redisClient.connect();


This defines a client for key type String and value type String.

Now what do I do when I need to handle multiple Redis objects, like not only String/String, but for example multiple sets with different types?

Should I create a different connection for each?

I have tried to use the reactive templates but get into the same problem i that I would create multiple instances with different types.



When dealing with transactions I don't understand how to use a transaction across a single template.

For example, I want to insert a serialized post into a Redis store like:



Key postId | Value <post> 


But in one transaction, I also want to add the postId in a set that represents a feed:



Key topic | Value <set with post Id's>


All examples that I found perform transactions on the same template, but I have no idea how to continue with this.

Pointers are appreciated.







java redis lettuce






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 24 '18 at 14:17









TraceTrace

8,12653686




8,12653686













  • How about using StatefulRedisConnection <Object, Object>

    – Sid Malani
    Nov 30 '18 at 3:02











  • A working example would be appreciated. I currently solved it by removing the map postId-post as I don’t really need it and store posts directly in the zset. But it’s just a lucky coincidence. I realise Redis is not relational... but somehow I thought it would be capable of this.

    – Trace
    Nov 30 '18 at 9:09



















  • How about using StatefulRedisConnection <Object, Object>

    – Sid Malani
    Nov 30 '18 at 3:02











  • A working example would be appreciated. I currently solved it by removing the map postId-post as I don’t really need it and store posts directly in the zset. But it’s just a lucky coincidence. I realise Redis is not relational... but somehow I thought it would be capable of this.

    – Trace
    Nov 30 '18 at 9:09

















How about using StatefulRedisConnection <Object, Object>

– Sid Malani
Nov 30 '18 at 3:02





How about using StatefulRedisConnection <Object, Object>

– Sid Malani
Nov 30 '18 at 3:02













A working example would be appreciated. I currently solved it by removing the map postId-post as I don’t really need it and store posts directly in the zset. But it’s just a lucky coincidence. I realise Redis is not relational... but somehow I thought it would be capable of this.

– Trace
Nov 30 '18 at 9:09





A working example would be appreciated. I currently solved it by removing the map postId-post as I don’t really need it and store posts directly in the zset. But it’s just a lucky coincidence. I realise Redis is not relational... but somehow I thought it would be capable of this.

– Trace
Nov 30 '18 at 9:09












1 Answer
1






active

oldest

votes


















1





+50









The String, String part while defining a StatefulRedisConnection is applicable only to the codec being used. When you define:



StatefulRedisConnection<String, String> connection = redisClient.connect();


a predefined StringCodec is used by RedisClient to transfer data between java client and Redis.



You can use your custom codec here if you want to change how keys and values are encoded and decoded while transferring data to and fro between Redis and Client. More detail about RedisCodec here.



For the purpose of your problem. You still should be able to use same connection for different types of operations, provided the key and value(s) are all Strings. For keys and values which are of Object type, you should serialize them and pass the serialized results to above connection.



Here is an example snippet (didn't run/test this).



RedisClient redisClient = RedisClient.create("redis://password@localhost:6379/0");
StatefulRedisConnection<String, String> connection = redisClient.connect();
RedisCommands<String, String> syncCommands = connection.sync();
syncCommands.multi();
syncCommands.sadd("topics", "value1", "value2", "value3"); // Puts topics in the set
syncCommands.set("posts", "serialized post"); // Puts serialized post
syncCommands.exec();


Hope this helps.






share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53459068%2flettuce-multiple-reactive-redis-stores-and-transactions-across-stores%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1





    +50









    The String, String part while defining a StatefulRedisConnection is applicable only to the codec being used. When you define:



    StatefulRedisConnection<String, String> connection = redisClient.connect();


    a predefined StringCodec is used by RedisClient to transfer data between java client and Redis.



    You can use your custom codec here if you want to change how keys and values are encoded and decoded while transferring data to and fro between Redis and Client. More detail about RedisCodec here.



    For the purpose of your problem. You still should be able to use same connection for different types of operations, provided the key and value(s) are all Strings. For keys and values which are of Object type, you should serialize them and pass the serialized results to above connection.



    Here is an example snippet (didn't run/test this).



    RedisClient redisClient = RedisClient.create("redis://password@localhost:6379/0");
    StatefulRedisConnection<String, String> connection = redisClient.connect();
    RedisCommands<String, String> syncCommands = connection.sync();
    syncCommands.multi();
    syncCommands.sadd("topics", "value1", "value2", "value3"); // Puts topics in the set
    syncCommands.set("posts", "serialized post"); // Puts serialized post
    syncCommands.exec();


    Hope this helps.






    share|improve this answer






























      1





      +50









      The String, String part while defining a StatefulRedisConnection is applicable only to the codec being used. When you define:



      StatefulRedisConnection<String, String> connection = redisClient.connect();


      a predefined StringCodec is used by RedisClient to transfer data between java client and Redis.



      You can use your custom codec here if you want to change how keys and values are encoded and decoded while transferring data to and fro between Redis and Client. More detail about RedisCodec here.



      For the purpose of your problem. You still should be able to use same connection for different types of operations, provided the key and value(s) are all Strings. For keys and values which are of Object type, you should serialize them and pass the serialized results to above connection.



      Here is an example snippet (didn't run/test this).



      RedisClient redisClient = RedisClient.create("redis://password@localhost:6379/0");
      StatefulRedisConnection<String, String> connection = redisClient.connect();
      RedisCommands<String, String> syncCommands = connection.sync();
      syncCommands.multi();
      syncCommands.sadd("topics", "value1", "value2", "value3"); // Puts topics in the set
      syncCommands.set("posts", "serialized post"); // Puts serialized post
      syncCommands.exec();


      Hope this helps.






      share|improve this answer




























        1





        +50







        1





        +50



        1




        +50





        The String, String part while defining a StatefulRedisConnection is applicable only to the codec being used. When you define:



        StatefulRedisConnection<String, String> connection = redisClient.connect();


        a predefined StringCodec is used by RedisClient to transfer data between java client and Redis.



        You can use your custom codec here if you want to change how keys and values are encoded and decoded while transferring data to and fro between Redis and Client. More detail about RedisCodec here.



        For the purpose of your problem. You still should be able to use same connection for different types of operations, provided the key and value(s) are all Strings. For keys and values which are of Object type, you should serialize them and pass the serialized results to above connection.



        Here is an example snippet (didn't run/test this).



        RedisClient redisClient = RedisClient.create("redis://password@localhost:6379/0");
        StatefulRedisConnection<String, String> connection = redisClient.connect();
        RedisCommands<String, String> syncCommands = connection.sync();
        syncCommands.multi();
        syncCommands.sadd("topics", "value1", "value2", "value3"); // Puts topics in the set
        syncCommands.set("posts", "serialized post"); // Puts serialized post
        syncCommands.exec();


        Hope this helps.






        share|improve this answer















        The String, String part while defining a StatefulRedisConnection is applicable only to the codec being used. When you define:



        StatefulRedisConnection<String, String> connection = redisClient.connect();


        a predefined StringCodec is used by RedisClient to transfer data between java client and Redis.



        You can use your custom codec here if you want to change how keys and values are encoded and decoded while transferring data to and fro between Redis and Client. More detail about RedisCodec here.



        For the purpose of your problem. You still should be able to use same connection for different types of operations, provided the key and value(s) are all Strings. For keys and values which are of Object type, you should serialize them and pass the serialized results to above connection.



        Here is an example snippet (didn't run/test this).



        RedisClient redisClient = RedisClient.create("redis://password@localhost:6379/0");
        StatefulRedisConnection<String, String> connection = redisClient.connect();
        RedisCommands<String, String> syncCommands = connection.sync();
        syncCommands.multi();
        syncCommands.sadd("topics", "value1", "value2", "value3"); // Puts topics in the set
        syncCommands.set("posts", "serialized post"); // Puts serialized post
        syncCommands.exec();


        Hope this helps.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Dec 3 '18 at 20:38

























        answered Dec 3 '18 at 20:30









        faceface

        1,1951020




        1,1951020






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53459068%2flettuce-multiple-reactive-redis-stores-and-transactions-across-stores%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

            Calculate evaluation metrics using cross_val_predict sklearn

            Insert data from modal to MySQL (multiple modal on website)