Use memcached for laravel on multiple sites












0















On a server I have about 25 websites running the same laravel based backed. I want to change the session&cache driver from file to memcached. But this causes a cached data mixup. e.g. I see the menu from site 1 on site 2.



How can I use memcached on multiple sites?










share|improve this question



























    0















    On a server I have about 25 websites running the same laravel based backed. I want to change the session&cache driver from file to memcached. But this causes a cached data mixup. e.g. I see the menu from site 1 on site 2.



    How can I use memcached on multiple sites?










    share|improve this question

























      0












      0








      0








      On a server I have about 25 websites running the same laravel based backed. I want to change the session&cache driver from file to memcached. But this causes a cached data mixup. e.g. I see the menu from site 1 on site 2.



      How can I use memcached on multiple sites?










      share|improve this question














      On a server I have about 25 websites running the same laravel based backed. I want to change the session&cache driver from file to memcached. But this causes a cached data mixup. e.g. I see the menu from site 1 on site 2.



      How can I use memcached on multiple sites?







      laravel memcached






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Dec 30 '18 at 9:03









      PatrickPatrick

      63




      63
























          3 Answers
          3






          active

          oldest

          votes


















          0














          I think the only possible way of doing this would be to have unique cache keys for each website.



          It could be based on a unique website identifier (id or url).
          I think the logic to retrieve the cache keys could also be encapsulated a bit. In my current setup, I have something a little similar whereby the cache keys are different depending on the locale.



          protected function getCacheKey($key, $locale = null)
          {
          $prefix = '__app_cache_';
          $suffix = $locale ? "_$locale" : '';

          return $prefix . $key . $suffix;
          }


          For example, your logic could be something like this:



          protected function getCacheKey($key, Website $website)
          {
          $prefix = '__app_cache_';
          $suffix = "_{$website->id}" : '';

          return $prefix . $key . $suffix;
          }


          and then to get the menu cache key, you would do something like this:



          $cache->getCacheKey($menu, $website); 


          This is, obviously, just an example and needs to be adapted to your specific needs.






          share|improve this answer

































            0














            Based on the idea of a prefix I found a nicer solution for Laravel. But somehow I cant find this on the internet.



            In the cache.php file I added an extra key options with the memcached prefix option that is available in the memcached documentation:



            'memcached' => [
            'driver' => 'memcached',
            'servers' => [
            [
            'host' => '127.0.0.1', 'port' => 11211, 'weight' => 100
            ],
            ],
            'options' => [
            Memcached::OPT_PREFIX_KEY => "websitename",
            ],
            ],


            Now all the sites can run without issues






            share|improve this answer































              0














              Brother, If you haven't used Memcached with Laravel then I would prefer to go with Redis, It has more good speed in easy to work with Laravel.






              share|improve this answer
























              • @Patrick also you can use this with multiple key based the key pattern, Like Domain name and key and unique id, somedomain_key_uniqueID.

                – Muhammad Adil
                Dec 31 '18 at 8:40











              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%2f53976373%2fuse-memcached-for-laravel-on-multiple-sites%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              0














              I think the only possible way of doing this would be to have unique cache keys for each website.



              It could be based on a unique website identifier (id or url).
              I think the logic to retrieve the cache keys could also be encapsulated a bit. In my current setup, I have something a little similar whereby the cache keys are different depending on the locale.



              protected function getCacheKey($key, $locale = null)
              {
              $prefix = '__app_cache_';
              $suffix = $locale ? "_$locale" : '';

              return $prefix . $key . $suffix;
              }


              For example, your logic could be something like this:



              protected function getCacheKey($key, Website $website)
              {
              $prefix = '__app_cache_';
              $suffix = "_{$website->id}" : '';

              return $prefix . $key . $suffix;
              }


              and then to get the menu cache key, you would do something like this:



              $cache->getCacheKey($menu, $website); 


              This is, obviously, just an example and needs to be adapted to your specific needs.






              share|improve this answer






























                0














                I think the only possible way of doing this would be to have unique cache keys for each website.



                It could be based on a unique website identifier (id or url).
                I think the logic to retrieve the cache keys could also be encapsulated a bit. In my current setup, I have something a little similar whereby the cache keys are different depending on the locale.



                protected function getCacheKey($key, $locale = null)
                {
                $prefix = '__app_cache_';
                $suffix = $locale ? "_$locale" : '';

                return $prefix . $key . $suffix;
                }


                For example, your logic could be something like this:



                protected function getCacheKey($key, Website $website)
                {
                $prefix = '__app_cache_';
                $suffix = "_{$website->id}" : '';

                return $prefix . $key . $suffix;
                }


                and then to get the menu cache key, you would do something like this:



                $cache->getCacheKey($menu, $website); 


                This is, obviously, just an example and needs to be adapted to your specific needs.






                share|improve this answer




























                  0












                  0








                  0







                  I think the only possible way of doing this would be to have unique cache keys for each website.



                  It could be based on a unique website identifier (id or url).
                  I think the logic to retrieve the cache keys could also be encapsulated a bit. In my current setup, I have something a little similar whereby the cache keys are different depending on the locale.



                  protected function getCacheKey($key, $locale = null)
                  {
                  $prefix = '__app_cache_';
                  $suffix = $locale ? "_$locale" : '';

                  return $prefix . $key . $suffix;
                  }


                  For example, your logic could be something like this:



                  protected function getCacheKey($key, Website $website)
                  {
                  $prefix = '__app_cache_';
                  $suffix = "_{$website->id}" : '';

                  return $prefix . $key . $suffix;
                  }


                  and then to get the menu cache key, you would do something like this:



                  $cache->getCacheKey($menu, $website); 


                  This is, obviously, just an example and needs to be adapted to your specific needs.






                  share|improve this answer















                  I think the only possible way of doing this would be to have unique cache keys for each website.



                  It could be based on a unique website identifier (id or url).
                  I think the logic to retrieve the cache keys could also be encapsulated a bit. In my current setup, I have something a little similar whereby the cache keys are different depending on the locale.



                  protected function getCacheKey($key, $locale = null)
                  {
                  $prefix = '__app_cache_';
                  $suffix = $locale ? "_$locale" : '';

                  return $prefix . $key . $suffix;
                  }


                  For example, your logic could be something like this:



                  protected function getCacheKey($key, Website $website)
                  {
                  $prefix = '__app_cache_';
                  $suffix = "_{$website->id}" : '';

                  return $prefix . $key . $suffix;
                  }


                  and then to get the menu cache key, you would do something like this:



                  $cache->getCacheKey($menu, $website); 


                  This is, obviously, just an example and needs to be adapted to your specific needs.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Dec 30 '18 at 10:02

























                  answered Dec 30 '18 at 9:21









                  MozammilMozammil

                  5,096419




                  5,096419

























                      0














                      Based on the idea of a prefix I found a nicer solution for Laravel. But somehow I cant find this on the internet.



                      In the cache.php file I added an extra key options with the memcached prefix option that is available in the memcached documentation:



                      'memcached' => [
                      'driver' => 'memcached',
                      'servers' => [
                      [
                      'host' => '127.0.0.1', 'port' => 11211, 'weight' => 100
                      ],
                      ],
                      'options' => [
                      Memcached::OPT_PREFIX_KEY => "websitename",
                      ],
                      ],


                      Now all the sites can run without issues






                      share|improve this answer




























                        0














                        Based on the idea of a prefix I found a nicer solution for Laravel. But somehow I cant find this on the internet.



                        In the cache.php file I added an extra key options with the memcached prefix option that is available in the memcached documentation:



                        'memcached' => [
                        'driver' => 'memcached',
                        'servers' => [
                        [
                        'host' => '127.0.0.1', 'port' => 11211, 'weight' => 100
                        ],
                        ],
                        'options' => [
                        Memcached::OPT_PREFIX_KEY => "websitename",
                        ],
                        ],


                        Now all the sites can run without issues






                        share|improve this answer


























                          0












                          0








                          0







                          Based on the idea of a prefix I found a nicer solution for Laravel. But somehow I cant find this on the internet.



                          In the cache.php file I added an extra key options with the memcached prefix option that is available in the memcached documentation:



                          'memcached' => [
                          'driver' => 'memcached',
                          'servers' => [
                          [
                          'host' => '127.0.0.1', 'port' => 11211, 'weight' => 100
                          ],
                          ],
                          'options' => [
                          Memcached::OPT_PREFIX_KEY => "websitename",
                          ],
                          ],


                          Now all the sites can run without issues






                          share|improve this answer













                          Based on the idea of a prefix I found a nicer solution for Laravel. But somehow I cant find this on the internet.



                          In the cache.php file I added an extra key options with the memcached prefix option that is available in the memcached documentation:



                          'memcached' => [
                          'driver' => 'memcached',
                          'servers' => [
                          [
                          'host' => '127.0.0.1', 'port' => 11211, 'weight' => 100
                          ],
                          ],
                          'options' => [
                          Memcached::OPT_PREFIX_KEY => "websitename",
                          ],
                          ],


                          Now all the sites can run without issues







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Dec 31 '18 at 8:37









                          PatrickPatrick

                          63




                          63























                              0














                              Brother, If you haven't used Memcached with Laravel then I would prefer to go with Redis, It has more good speed in easy to work with Laravel.






                              share|improve this answer
























                              • @Patrick also you can use this with multiple key based the key pattern, Like Domain name and key and unique id, somedomain_key_uniqueID.

                                – Muhammad Adil
                                Dec 31 '18 at 8:40
















                              0














                              Brother, If you haven't used Memcached with Laravel then I would prefer to go with Redis, It has more good speed in easy to work with Laravel.






                              share|improve this answer
























                              • @Patrick also you can use this with multiple key based the key pattern, Like Domain name and key and unique id, somedomain_key_uniqueID.

                                – Muhammad Adil
                                Dec 31 '18 at 8:40














                              0












                              0








                              0







                              Brother, If you haven't used Memcached with Laravel then I would prefer to go with Redis, It has more good speed in easy to work with Laravel.






                              share|improve this answer













                              Brother, If you haven't used Memcached with Laravel then I would prefer to go with Redis, It has more good speed in easy to work with Laravel.







                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Dec 31 '18 at 8:39









                              Muhammad AdilMuhammad Adil

                              468




                              468













                              • @Patrick also you can use this with multiple key based the key pattern, Like Domain name and key and unique id, somedomain_key_uniqueID.

                                – Muhammad Adil
                                Dec 31 '18 at 8:40



















                              • @Patrick also you can use this with multiple key based the key pattern, Like Domain name and key and unique id, somedomain_key_uniqueID.

                                – Muhammad Adil
                                Dec 31 '18 at 8:40

















                              @Patrick also you can use this with multiple key based the key pattern, Like Domain name and key and unique id, somedomain_key_uniqueID.

                              – Muhammad Adil
                              Dec 31 '18 at 8:40





                              @Patrick also you can use this with multiple key based the key pattern, Like Domain name and key and unique id, somedomain_key_uniqueID.

                              – Muhammad Adil
                              Dec 31 '18 at 8:40


















                              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%2f53976373%2fuse-memcached-for-laravel-on-multiple-sites%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

                              Monofisismo

                              Angular Downloading a file using contenturl with Basic Authentication

                              Olmecas