{"action":"created","issue":{"url":"https://api.github.com/repos/silverstripe/silverstripe-framework/issues/6358","repository_url":"https://api.github.com/repos/silverstripe/silverstripe-framework","labels_url":"https://api.github.com/repos/silverstripe/silverstripe-framework/issues/6358/labels{/name}","comments_url":"https://api.github.com/repos/silverstripe/silverstripe-framework/issues/6358/comments","events_url":"https://api.github.com/repos/silverstripe/silverstripe-framework/issues/6358/events","html_url":"https://github.com/silverstripe/silverstripe-framework/issues/6358","id":192995801,"number":6358,"title":"caching on shared environment - add base URL to cachekey to prevent cross-pollination","user":{"login":"sunnysideup","id":167154,"avatar_url":"https://avatars.githubusercontent.com/u/167154?v=3","gravatar_id":"","url":"https://api.github.com/users/sunnysideup","html_url":"https://github.com/sunnysideup","followers_url":"https://api.github.com/users/sunnysideup/followers","following_url":"https://api.github.com/users/sunnysideup/following{/other_user}","gists_url":"https://api.github.com/users/sunnysideup/gists{/gist_id}","starred_url":"https://api.github.com/users/sunnysideup/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sunnysideup/subscriptions","organizations_url":"https://api.github.com/users/sunnysideup/orgs","repos_url":"https://api.github.com/users/sunnysideup/repos","events_url":"https://api.github.com/users/sunnysideup/events{/privacy}","received_events_url":"https://api.github.com/users/sunnysideup/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":5,"created_at":"2016-12-01T23:51:09Z","updated_at":"2016-12-06T17:46:26Z","closed_at":null,"body":"We have been using caching a lot lately - like this:\r\n\r\nhttps://github.com/silverstripe/silverstripe-framework/blob/67496d3eb017dae09d0e55c607d2e80a60654e70/tests/php/Core/CacheTest.php#L10-L15\r\n\r\nThat works great - but there is a catch (there are many, but this is a big one):\r\n\r\nUnless you specifically make sure that each site uses its own caching system (be it APC / temp files / Memcache) any matching cache keys are going to cross-pollinate.  Of course this only applies if two sites are running on the same server and are using the same key - but it still applies even if they use separate file systems (apc / memcache). \r\n\r\nI confirmed this theory by creating the following task:\r\n\r\n\r\n```php\r\n\r\n<?php\r\n\r\n\r\nclass CacheTest extends BuildTask\r\n{\r\n    protected $title = 'Test Silverstripe Cache';\r\n\r\n    protected $description = 'Basic test for the silverstripe cache. It will show the date and time the cache was made.';\r\n\r\n    public function run($request)\r\n    {\r\n        $cachekey = 'foo';\r\n        $cache = SS_Cache::factory($cachekey);\r\n        if (!($result = $cache->load($cachekey))) {\r\n            $result = date('Y-m-d H:i:s');;\r\n            $cache->save($result, $cachekey);\r\n        }\r\n        echo $result;\r\n    }\r\n\r\n}\r\n\r\n```\r\nyou can view this task by visiting: www.mysite.co.nz/dev/tasks/CacheTest.\r\n\r\nThis task returns the exact same second, if:\r\n\r\n - websites are the same server even if there are  in completely separate accounts\r\n - you are running memcache cache or apc cache (not tested for file based caching) \r\n\r\nSo, what I would recommend is that we simply add the URL to the caching key, or at least a simplified version of it.\r\n\r\nhttps://github.com/silverstripe/silverstripe-framework/blob/67496d3eb017dae09d0e55c607d2e80a60654e70/src/Core/Cache.php#L175\r\n\r\nand add to the caching id ($for): \r\n\r\n```php\r\n$prepend = hash(\"crc32b\", Director::absoluteBaseURL());\r\n$for = $prepend.$for;\r\n```\r\n\r\nAs a second rate alternative, I feel that the documentation should at least point out this risk.\r\n\r\n\r\n\r\nMy apologies in advance if I have not completely understood how this works. \r\n"},"comment":{"url":"https://api.github.com/repos/silverstripe/silverstripe-framework/issues/comments/265219497","html_url":"https://github.com/silverstripe/silverstripe-framework/issues/6358#issuecomment-265219497","issue_url":"https://api.github.com/repos/silverstripe/silverstripe-framework/issues/6358","id":265219497,"user":{"login":"sunnysideup","id":167154,"avatar_url":"https://avatars.githubusercontent.com/u/167154?v=3","gravatar_id":"","url":"https://api.github.com/users/sunnysideup","html_url":"https://github.com/sunnysideup","followers_url":"https://api.github.com/users/sunnysideup/followers","following_url":"https://api.github.com/users/sunnysideup/following{/other_user}","gists_url":"https://api.github.com/users/sunnysideup/gists{/gist_id}","starred_url":"https://api.github.com/users/sunnysideup/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sunnysideup/subscriptions","organizations_url":"https://api.github.com/users/sunnysideup/orgs","repos_url":"https://api.github.com/users/sunnysideup/repos","events_url":"https://api.github.com/users/sunnysideup/events{/privacy}","received_events_url":"https://api.github.com/users/sunnysideup/received_events","type":"User","site_admin":false},"created_at":"2016-12-06T17:46:26Z","updated_at":"2016-12-06T17:46:26Z","body":"as an aside: I got my hosting team to set up memcache and I am now running four instances of it on different ports (i.e. separating them), but it does not seem to be working very well at all.\r\n\r\nhttp://stackoverflow.com/questions/40964315/stop-memcache-from-removing-data-in-php-silverstripe-zend-cache\r\n\r\nFor the APC cache I found the same thing: after a minute or so the cache expires. \r\n"}}
{"actor":{"display_login":"sunnysideup"}}