We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 38926
    • 33 Posts
    Got a question about the viability of using the modx cache to store data for use in a shopping system.

    Basically, I've got the simplecart module, but it doesn't support "extra information" about a product, and the products I'm adding to the cart comes with a collection of dates arrays.

    I've got it all set up, and the dates makes it to the actual cart page as a JSON string, but I need to bring all the dates along for the ride through the shops checkout process.

    I've ruled out using a cookie, since the dates is used in calculating the prices and I don't want the user fiddling with them. I was planning on using modx cache system to store the data, but I would like to know if anyone knows of possible pitfalls in using the modx cache system to store important data (temporarily), or if there is some other method I should be going for in a situation like this?

    Any help will be much appreciated.
      • 22303 MODX Staff
      • 10,725 Posts
      You could use the cache, but this is vulnerable to cache clearing processes. You might look into storing them in the MODX registry, which is a simplified message bus that can store many kinds of messages (including executable PHP, JSON, etc.) into a "register". These messages, though transient, are kept until they are read and removed by the reader explicitly, and can be stored in different implementations (MODX comes with a file and a database register implementation; I would recommend the database implementation for your purposes). The main benefit of using this over sessions is that you do not need to worry about maintaining the session data if the session changes for the user (i.e. they shop without being logged in and have to login at some point during the checkout process).

      See http://rtfm.modx.com/display/revolution20/modRegistry for additional information on this part of the MODX API, and let us know if you have any questions. [ed. note: opengeek last edited this post 14 years, 3 months ago.]
        • 38926
        • 33 Posts
        Thank you very much, that sounds exactly like something I could use. I will look into that once rtfm.modx.com is online again wink
          • 38926
          • 33 Posts
          Okay, so I've been medling with the modRegistry for the entire day, but I can't quite get it to do as I want. Most likely I've misunderstood something in the instructions.

          So, on one page I've got a snippet that does this:

          <?php
          
          $modx->getService('registry', 'registry.modRegistry');
          
          $modx->registry->addRegister('productDates', 'registry.modDbRegister', array('directory' => 'productDates'));
          
          $modx->registry->productDates->connect();
          $modx->registry->productDates->subscribe("/dates/");
          $modx->registry->productDates->send("/dates/", array("beer1", "beer2", "beer3"));
          
          $msgs = $modx->registry->productDates->read(array(
              'poll_limit' => 1,
              'msg_limit' => 5
          ));
          
          var_dump($msgs);
          


          It outputs:
          array(3) { [0]=> string(5) "beer1" [1]=> string(5) "beer2" [2]=> string(5) "beer3" }

          Thats all good.

          Now, I got to another page where I try to access that registry with this snippet:

          <?php 
          
          $modx->getService('registry', 'registry.modRegistry');
          
          $modx->registry->addRegister('productDates', 'registry.modDbRegister');
          
          $modx->registry->productDates->connect();	
          $modx->registry->productDates->subscribe("/dates/");
          
          $msgs = $modx->registry->productDates->read(array(
          	    'poll_limit' => 1,
          	    'msg_limit' => 5
          	));
          
          var_dump($msgs);


          But all I get is an empty array:

          array(0) { }


          What am I missing? [ed. note: cruiser last edited this post 14 years, 3 months ago.]
            • 22303 MODX Staff
            • 10,725 Posts
            When you read() the message( s ) you send in the first script, you are removing those messages. When read()'ing from a register, the default behavior is for the read operation to also remove the message, similar to any basic message queue. If you want to read them to verify they are there without removing them, add the remove_read => false option to the read() call, e.g.
            $msgs = $modx->registry->productDates->read(array(
                'poll_limit' => 1,
                'msg_limit' => 5,
                'remove_read' => false
            ));
              • 38926
              • 33 Posts
              Ah I see. Now that I look at it closer, the registry section on rtfm didn't mention that remove_thread was set to true as standard. Guess it's my own fault for not using remove_read = false sooner to try it out, but I'm not used to storages that deletes their content upon reading them tongue

              Thank you opengeek. It works like a charm now.