Redis: clean up members of ZSET












0















I'm currently studying Redis, and have the following case:



enter image description here



So what I have is a sorted set by google place id, for which all posts are sorted from recent to older.
The first page that is requested fetches posts < current timestamp.
When a cursor is sent to the backend, this cursor is a simple timestamp that indicates from where to fetch the next posts from the ZSET.

The query to retrieve posts by place id would be:



ZREVRANGEBYSCORE <gplaceId> <cur_timestamp> -INF WITHSCORES LIMIT <offset:timestamp as from where to fetch> <count:number of posts>


My question is what is the recommended way to clean up members of the ZSET.

Since I want to use Redis as a cache, I would prefer to limit the number of posts per place for example up until 50. When places get new posts when already 50 posts have been added to the set, I want to drop the last post from the set.



Of course I realise that I could make a manual check on every insert and perform operations as such, but I wonder if Redis has a recommended way of performing this cleanup. Alternatively I could create a scheduler for this, but I would prefer not having to do this.










share|improve this question



























    0















    I'm currently studying Redis, and have the following case:



    enter image description here



    So what I have is a sorted set by google place id, for which all posts are sorted from recent to older.
    The first page that is requested fetches posts < current timestamp.
    When a cursor is sent to the backend, this cursor is a simple timestamp that indicates from where to fetch the next posts from the ZSET.

    The query to retrieve posts by place id would be:



    ZREVRANGEBYSCORE <gplaceId> <cur_timestamp> -INF WITHSCORES LIMIT <offset:timestamp as from where to fetch> <count:number of posts>


    My question is what is the recommended way to clean up members of the ZSET.

    Since I want to use Redis as a cache, I would prefer to limit the number of posts per place for example up until 50. When places get new posts when already 50 posts have been added to the set, I want to drop the last post from the set.



    Of course I realise that I could make a manual check on every insert and perform operations as such, but I wonder if Redis has a recommended way of performing this cleanup. Alternatively I could create a scheduler for this, but I would prefer not having to do this.










    share|improve this question

























      0












      0








      0








      I'm currently studying Redis, and have the following case:



      enter image description here



      So what I have is a sorted set by google place id, for which all posts are sorted from recent to older.
      The first page that is requested fetches posts < current timestamp.
      When a cursor is sent to the backend, this cursor is a simple timestamp that indicates from where to fetch the next posts from the ZSET.

      The query to retrieve posts by place id would be:



      ZREVRANGEBYSCORE <gplaceId> <cur_timestamp> -INF WITHSCORES LIMIT <offset:timestamp as from where to fetch> <count:number of posts>


      My question is what is the recommended way to clean up members of the ZSET.

      Since I want to use Redis as a cache, I would prefer to limit the number of posts per place for example up until 50. When places get new posts when already 50 posts have been added to the set, I want to drop the last post from the set.



      Of course I realise that I could make a manual check on every insert and perform operations as such, but I wonder if Redis has a recommended way of performing this cleanup. Alternatively I could create a scheduler for this, but I would prefer not having to do this.










      share|improve this question














      I'm currently studying Redis, and have the following case:



      enter image description here



      So what I have is a sorted set by google place id, for which all posts are sorted from recent to older.
      The first page that is requested fetches posts < current timestamp.
      When a cursor is sent to the backend, this cursor is a simple timestamp that indicates from where to fetch the next posts from the ZSET.

      The query to retrieve posts by place id would be:



      ZREVRANGEBYSCORE <gplaceId> <cur_timestamp> -INF WITHSCORES LIMIT <offset:timestamp as from where to fetch> <count:number of posts>


      My question is what is the recommended way to clean up members of the ZSET.

      Since I want to use Redis as a cache, I would prefer to limit the number of posts per place for example up until 50. When places get new posts when already 50 posts have been added to the set, I want to drop the last post from the set.



      Of course I realise that I could make a manual check on every insert and perform operations as such, but I wonder if Redis has a recommended way of performing this cleanup. Alternatively I could create a scheduler for this, but I would prefer not having to do this.







      redis






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 14 '18 at 19:52









      TraceTrace

      8,18553687




      8,18553687
























          1 Answer
          1






          active

          oldest

          votes


















          0














          Unfortunately Redis sorted set do not come with an out of the box feature for this. If sorted sets allowed a max size attribute with a configurable eviction strategy - you could have avoided some extra work.



          See this related question:
          How to specify Redis Sorted Set a fixed size?



          In absence of such a feature, the two approaches you mentioned are right.




          1. You can replace inserts with a transaction : insert, check size, delete if > 50


          2. A thread that checks size of the set and trims it periodically







          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%2f53307809%2fredis-clean-up-members-of-zset%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









            0














            Unfortunately Redis sorted set do not come with an out of the box feature for this. If sorted sets allowed a max size attribute with a configurable eviction strategy - you could have avoided some extra work.



            See this related question:
            How to specify Redis Sorted Set a fixed size?



            In absence of such a feature, the two approaches you mentioned are right.




            1. You can replace inserts with a transaction : insert, check size, delete if > 50


            2. A thread that checks size of the set and trims it periodically







            share|improve this answer




























              0














              Unfortunately Redis sorted set do not come with an out of the box feature for this. If sorted sets allowed a max size attribute with a configurable eviction strategy - you could have avoided some extra work.



              See this related question:
              How to specify Redis Sorted Set a fixed size?



              In absence of such a feature, the two approaches you mentioned are right.




              1. You can replace inserts with a transaction : insert, check size, delete if > 50


              2. A thread that checks size of the set and trims it periodically







              share|improve this answer


























                0












                0








                0







                Unfortunately Redis sorted set do not come with an out of the box feature for this. If sorted sets allowed a max size attribute with a configurable eviction strategy - you could have avoided some extra work.



                See this related question:
                How to specify Redis Sorted Set a fixed size?



                In absence of such a feature, the two approaches you mentioned are right.




                1. You can replace inserts with a transaction : insert, check size, delete if > 50


                2. A thread that checks size of the set and trims it periodically







                share|improve this answer













                Unfortunately Redis sorted set do not come with an out of the box feature for this. If sorted sets allowed a max size attribute with a configurable eviction strategy - you could have avoided some extra work.



                See this related question:
                How to specify Redis Sorted Set a fixed size?



                In absence of such a feature, the two approaches you mentioned are right.




                1. You can replace inserts with a transaction : insert, check size, delete if > 50


                2. A thread that checks size of the set and trims it periodically








                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 14 '18 at 21:32









                rainhackerrainhacker

                1188




                1188
































                    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%2f53307809%2fredis-clean-up-members-of-zset%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

                    Florida Star v. B. J. F.

                    Danny Elfman

                    Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues