{"action":"created","comment":{"url":"https://api.github.com/repos/Imhonet-ru/Connection/pulls/comments/59241573","id":59241573,"diff_hunk":"@@ -0,0 +1,333 @@\n+<?php\n+\n+namespace Imhonet\\Connection\\Cache;\n+\n+use Imhonet\\Connection\\Resource\\IResource;\n+use Imhonet\\Connection\\Query\\Memcached;\n+use Imhonet\\Connection\\Query;\n+\n+class Cacher implements ICacher\n+{\n+    const DATA_EXPIRE = 31536000; // год\n+    const TAGS_EXPIRE = 31536000;\n+    const DEFAULT_EXPIRE = 600;\n+\n+    private $errors = array();\n+    private $cached_data = array();\n+    private $cached_keys = array();\n+    private $cached_tags = array();\n+\n+    private $main_resource;\n+    private $tags_resource;\n+\n+    private $expire = null;\n+    private $tags = array();\n+\n+    public static function instanceCacherMemcache($host, $port, $tags_host = null, $tags_port = null)\n+    {\n+        if ($tags_host === null || $tags_port === null) {\n+            $tags_host = $host;\n+            $tags_port = $port;\n+        }\n+\n+        $resource = \\Imhonet\\Connection\\Resource\\Factory::getInstance()\n+            ->setHost($host)\n+            ->setPort($port)\n+            ->getResource(\\Imhonet\\Connection\\Resource\\Factory::TYPE_MEMCACHED);\n+\n+        $tags_resource = \\Imhonet\\Connection\\Resource\\Factory::getInstance()\n+            ->setHost($tags_host)\n+            ->setPort($tags_port)\n+            ->getResource(\\Imhonet\\Connection\\Resource\\Factory::TYPE_MEMCACHED);\n+\n+        return new Cacher($resource, $tags_resource);\n+    }\n+\n+    public function __construct(IResource $main_resource, IResource $tag_resource)\n+    {\n+        $this->main_resource = $main_resource;\n+        $this->tags_resource = $tag_resource;\n+    }\n+\n+    /**\n+     * @param mixed $object\n+     * @return bool\n+     */\n+    public function isCacheable($object)\n+    {\n+        return $object instanceof ICachable;\n+    }\n+\n+    /**\n+     * @param string $key\n+     * @return bool\n+     */\n+    public function isCached($key)\n+    {\n+        if (!array_key_exists($key, $this->cached_keys)) {\n+            $this->_load(array($key => $key));\n+        }\n+\n+        return array_key_exists($key, $this->cached_data);\n+    }\n+\n+    /**\n+     * @param string $key\n+     * @return mixed\n+     * @throws CacheException\n+     */\n+    public function get($key)\n+    {\n+        if (!array_key_exists($key, $this->cached_keys)) {\n+            $this->_load(array($key => $key));\n+        }\n+\n+        if (array_key_exists($key, $this->errors)) {\n+            throw new CacheException($this->errors[$key]);\n+        }\n+\n+        return array_key_exists($key, $this->cached_data) ? $this->cached_data[$key] : null;\n+    }\n+\n+    /**\n+     * @param\n+     * @return int|null\n+     */\n+    public function set($key, $data, $tags = array(), $expire = null)\n+    {\n+        $this->lock($key, $tags, $expire);\n+\n+        $set_query = $this->createSetQuery($this->main_resource);\n+        $set_query->setData(array($this->generateDataKey($key) => array($data)));\n+        $set_query->setExpire(time() + Cacher::DATA_EXPIRE);\n+        $set_query->execute();\n+\n+        $this->cached_data[$key] = $data;\n+        $this->cached_keys[$key] = $key;\n+    }\n+\n+\n+    /**\n+     * @param\n+     * @return int|null\n+     */\n+    public function lock($key, $tags = array(), $expire = null)\n+    {\n+        if ($expire === null) {\n+            $expire = $this->expire !== null ? $this->expire : Cacher::DEFAULT_EXPIRE;\n+        }\n+\n+        if (empty($tags)) {\n+            $tags = $this->tags;\n+        }\n+\n+        $cache_stale = array($this->generateStaleKey($key) => array('tags' => $tags, 'timestamp' => time()));\n+\n+        $set_query = $this->createSetQuery($this->main_resource);\n+        $set_query->setData($cache_stale);\n+        $set_query->setExpire(time() + $expire);\n+        $set_query->execute();\n+\n+        $get_tags = array();\n+        $new_tags = array();\n+\n+        foreach ($tags as $tag) {\n+            if (!array_key_exists($tag, $this->cached_tags)) {\n+                $get_tags[$tag] = $this->generateTagKey($tag);\n+            }\n+        }\n+\n+        if (!empty($get_tags)) {\n+            $get_tags_query = $this->createGetQuery($this->tags_resource);\n+            $get_tags_query->setKeys($get_tags);\n+            $get_tags_data = $get_tags_query->execute();\n+\n+            foreach ($get_tags as $tag => $tag_key) {\n+                if (array_key_exists($tag_key, $get_tags_data)) {\n+                    $this->cached_tags[$tag] = $get_tags_data[$tag_key];\n+                    unset($get_tags[$tag]);\n+                } else {\n+                    $timestamp = time();\n+                    $this->cached_tags[$tag] = $timestamp;\n+                    $new_tags[$tag_key] = $timestamp;\n+                }\n+            }","path":"src/Cache/Cacher.php","position":154,"original_position":154,"commit_id":"a7269df822ac9909e3a3a58bf1d777c80444e4c0","original_commit_id":"a7269df822ac9909e3a3a58bf1d777c80444e4c0","user":{"login":"Imhoclub","id":5030990,"avatar_url":"https://avatars.githubusercontent.com/u/5030990?v=3","gravatar_id":"","url":"https://api.github.com/users/Imhoclub","html_url":"https://github.com/Imhoclub","followers_url":"https://api.github.com/users/Imhoclub/followers","following_url":"https://api.github.com/users/Imhoclub/following{/other_user}","gists_url":"https://api.github.com/users/Imhoclub/gists{/gist_id}","starred_url":"https://api.github.com/users/Imhoclub/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Imhoclub/subscriptions","organizations_url":"https://api.github.com/users/Imhoclub/orgs","repos_url":"https://api.github.com/users/Imhoclub/repos","events_url":"https://api.github.com/users/Imhoclub/events{/privacy}","received_events_url":"https://api.github.com/users/Imhoclub/received_events","type":"User","site_admin":false},"body":"похоже на array_diff_key","created_at":"2016-04-11T17:06:06Z","updated_at":"2016-04-11T17:06:06Z","html_url":"https://github.com/Imhonet-ru/Connection/pull/47#discussion_r59241573","pull_request_url":"https://api.github.com/repos/Imhonet-ru/Connection/pulls/47","_links":{"self":{"href":"https://api.github.com/repos/Imhonet-ru/Connection/pulls/comments/59241573"},"html":{"href":"https://github.com/Imhonet-ru/Connection/pull/47#discussion_r59241573"},"pull_request":{"href":"https://api.github.com/repos/Imhonet-ru/Connection/pulls/47"}}},"pull_request":{"url":"https://api.github.com/repos/Imhonet-ru/Connection/pulls/47","id":65429088,"html_url":"https://github.com/Imhonet-ru/Connection/pull/47","diff_url":"https://github.com/Imhonet-ru/Connection/pull/47.diff","patch_url":"https://github.com/Imhonet-ru/Connection/pull/47.patch","issue_url":"https://api.github.com/repos/Imhonet-ru/Connection/issues/47","number":47,"state":"open","locked":false,"title":"Cacher","user":{"login":"levon-a-akopian","id":396316,"avatar_url":"https://avatars.githubusercontent.com/u/396316?v=3","gravatar_id":"","url":"https://api.github.com/users/levon-a-akopian","html_url":"https://github.com/levon-a-akopian","followers_url":"https://api.github.com/users/levon-a-akopian/followers","following_url":"https://api.github.com/users/levon-a-akopian/following{/other_user}","gists_url":"https://api.github.com/users/levon-a-akopian/gists{/gist_id}","starred_url":"https://api.github.com/users/levon-a-akopian/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/levon-a-akopian/subscriptions","organizations_url":"https://api.github.com/users/levon-a-akopian/orgs","repos_url":"https://api.github.com/users/levon-a-akopian/repos","events_url":"https://api.github.com/users/levon-a-akopian/events{/privacy}","received_events_url":"https://api.github.com/users/levon-a-akopian/received_events","type":"User","site_admin":false},"body":"","created_at":"2016-04-06T08:41:24Z","updated_at":"2016-04-11T17:06:06Z","closed_at":null,"merged_at":null,"merge_commit_sha":"1ba11ba69dd2b85b70ff792580db1958b457f301","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/Imhonet-ru/Connection/pulls/47/commits","review_comments_url":"https://api.github.com/repos/Imhonet-ru/Connection/pulls/47/comments","review_comment_url":"https://api.github.com/repos/Imhonet-ru/Connection/pulls/comments{/number}","comments_url":"https://api.github.com/repos/Imhonet-ru/Connection/issues/47/comments","statuses_url":"https://api.github.com/repos/Imhonet-ru/Connection/statuses/a7269df822ac9909e3a3a58bf1d777c80444e4c0","head":{"label":"levon-a-akopian:master","ref":"master","sha":"a7269df822ac9909e3a3a58bf1d777c80444e4c0","user":{"login":"levon-a-akopian","id":396316,"avatar_url":"https://avatars.githubusercontent.com/u/396316?v=3","gravatar_id":"","url":"https://api.github.com/users/levon-a-akopian","html_url":"https://github.com/levon-a-akopian","followers_url":"https://api.github.com/users/levon-a-akopian/followers","following_url":"https://api.github.com/users/levon-a-akopian/following{/other_user}","gists_url":"https://api.github.com/users/levon-a-akopian/gists{/gist_id}","starred_url":"https://api.github.com/users/levon-a-akopian/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/levon-a-akopian/subscriptions","organizations_url":"https://api.github.com/users/levon-a-akopian/orgs","repos_url":"https://api.github.com/users/levon-a-akopian/repos","events_url":"https://api.github.com/users/levon-a-akopian/events{/privacy}","received_events_url":"https://api.github.com/users/levon-a-akopian/received_events","type":"User","site_admin":false},"repo":{"id":53850115,"name":"Connection","full_name":"levon-a-akopian/Connection","owner":{"login":"levon-a-akopian","id":396316,"avatar_url":"https://avatars.githubusercontent.com/u/396316?v=3","gravatar_id":"","url":"https://api.github.com/users/levon-a-akopian","html_url":"https://github.com/levon-a-akopian","followers_url":"https://api.github.com/users/levon-a-akopian/followers","following_url":"https://api.github.com/users/levon-a-akopian/following{/other_user}","gists_url":"https://api.github.com/users/levon-a-akopian/gists{/gist_id}","starred_url":"https://api.github.com/users/levon-a-akopian/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/levon-a-akopian/subscriptions","organizations_url":"https://api.github.com/users/levon-a-akopian/orgs","repos_url":"https://api.github.com/users/levon-a-akopian/repos","events_url":"https://api.github.com/users/levon-a-akopian/events{/privacy}","received_events_url":"https://api.github.com/users/levon-a-akopian/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/levon-a-akopian/Connection","description":"","fork":true,"url":"https://api.github.com/repos/levon-a-akopian/Connection","forks_url":"https://api.github.com/repos/levon-a-akopian/Connection/forks","keys_url":"https://api.github.com/repos/levon-a-akopian/Connection/keys{/key_id}","collaborators_url":"https://api.github.com/repos/levon-a-akopian/Connection/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/levon-a-akopian/Connection/teams","hooks_url":"https://api.github.com/repos/levon-a-akopian/Connection/hooks","issue_events_url":"https://api.github.com/repos/levon-a-akopian/Connection/issues/events{/number}","events_url":"https://api.github.com/repos/levon-a-akopian/Connection/events","assignees_url":"https://api.github.com/repos/levon-a-akopian/Connection/assignees{/user}","branches_url":"https://api.github.com/repos/levon-a-akopian/Connection/branches{/branch}","tags_url":"https://api.github.com/repos/levon-a-akopian/Connection/tags","blobs_url":"https://api.github.com/repos/levon-a-akopian/Connection/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/levon-a-akopian/Connection/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/levon-a-akopian/Connection/git/refs{/sha}","trees_url":"https://api.github.com/repos/levon-a-akopian/Connection/git/trees{/sha}","statuses_url":"https://api.github.com/repos/levon-a-akopian/Connection/statuses/{sha}","languages_url":"https://api.github.com/repos/levon-a-akopian/Connection/languages","stargazers_url":"https://api.github.com/repos/levon-a-akopian/Connection/stargazers","contributors_url":"https://api.github.com/repos/levon-a-akopian/Connection/contributors","subscribers_url":"https://api.github.com/repos/levon-a-akopian/Connection/subscribers","subscription_url":"https://api.github.com/repos/levon-a-akopian/Connection/subscription","commits_url":"https://api.github.com/repos/levon-a-akopian/Connection/commits{/sha}","git_commits_url":"https://api.github.com/repos/levon-a-akopian/Connection/git/commits{/sha}","comments_url":"https://api.github.com/repos/levon-a-akopian/Connection/comments{/number}","issue_comment_url":"https://api.github.com/repos/levon-a-akopian/Connection/issues/comments{/number}","contents_url":"https://api.github.com/repos/levon-a-akopian/Connection/contents/{+path}","compare_url":"https://api.github.com/repos/levon-a-akopian/Connection/compare/{base}...{head}","merges_url":"https://api.github.com/repos/levon-a-akopian/Connection/merges","archive_url":"https://api.github.com/repos/levon-a-akopian/Connection/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/levon-a-akopian/Connection/downloads","issues_url":"https://api.github.com/repos/levon-a-akopian/Connection/issues{/number}","pulls_url":"https://api.github.com/repos/levon-a-akopian/Connection/pulls{/number}","milestones_url":"https://api.github.com/repos/levon-a-akopian/Connection/milestones{/number}","notifications_url":"https://api.github.com/repos/levon-a-akopian/Connection/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/levon-a-akopian/Connection/labels{/name}","releases_url":"https://api.github.com/repos/levon-a-akopian/Connection/releases{/id}","deployments_url":"https://api.github.com/repos/levon-a-akopian/Connection/deployments","created_at":"2016-03-14T11:16:26Z","updated_at":"2016-03-14T11:16:27Z","pushed_at":"2016-04-07T09:49:50Z","git_url":"git://github.com/levon-a-akopian/Connection.git","ssh_url":"git@github.com:levon-a-akopian/Connection.git","clone_url":"https://github.com/levon-a-akopian/Connection.git","svn_url":"https://github.com/levon-a-akopian/Connection","homepage":null,"size":216,"stargazers_count":0,"watchers_count":0,"language":"PHP","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"Imhonet-ru:master","ref":"master","sha":"5c0bc7c93c07eed9e9fc1c0a402d989515e8f976","user":{"login":"Imhonet-ru","id":5031006,"avatar_url":"https://avatars.githubusercontent.com/u/5031006?v=3","gravatar_id":"","url":"https://api.github.com/users/Imhonet-ru","html_url":"https://github.com/Imhonet-ru","followers_url":"https://api.github.com/users/Imhonet-ru/followers","following_url":"https://api.github.com/users/Imhonet-ru/following{/other_user}","gists_url":"https://api.github.com/users/Imhonet-ru/gists{/gist_id}","starred_url":"https://api.github.com/users/Imhonet-ru/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Imhonet-ru/subscriptions","organizations_url":"https://api.github.com/users/Imhonet-ru/orgs","repos_url":"https://api.github.com/users/Imhonet-ru/repos","events_url":"https://api.github.com/users/Imhonet-ru/events{/privacy}","received_events_url":"https://api.github.com/users/Imhonet-ru/received_events","type":"Organization","site_admin":false},"repo":{"id":11476260,"name":"Connection","full_name":"Imhonet-ru/Connection","owner":{"login":"Imhonet-ru","id":5031006,"avatar_url":"https://avatars.githubusercontent.com/u/5031006?v=3","gravatar_id":"","url":"https://api.github.com/users/Imhonet-ru","html_url":"https://github.com/Imhonet-ru","followers_url":"https://api.github.com/users/Imhonet-ru/followers","following_url":"https://api.github.com/users/Imhonet-ru/following{/other_user}","gists_url":"https://api.github.com/users/Imhonet-ru/gists{/gist_id}","starred_url":"https://api.github.com/users/Imhonet-ru/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Imhonet-ru/subscriptions","organizations_url":"https://api.github.com/users/Imhonet-ru/orgs","repos_url":"https://api.github.com/users/Imhonet-ru/repos","events_url":"https://api.github.com/users/Imhonet-ru/events{/privacy}","received_events_url":"https://api.github.com/users/Imhonet-ru/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/Imhonet-ru/Connection","description":"","fork":false,"url":"https://api.github.com/repos/Imhonet-ru/Connection","forks_url":"https://api.github.com/repos/Imhonet-ru/Connection/forks","keys_url":"https://api.github.com/repos/Imhonet-ru/Connection/keys{/key_id}","collaborators_url":"https://api.github.com/repos/Imhonet-ru/Connection/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/Imhonet-ru/Connection/teams","hooks_url":"https://api.github.com/repos/Imhonet-ru/Connection/hooks","issue_events_url":"https://api.github.com/repos/Imhonet-ru/Connection/issues/events{/number}","events_url":"https://api.github.com/repos/Imhonet-ru/Connection/events","assignees_url":"https://api.github.com/repos/Imhonet-ru/Connection/assignees{/user}","branches_url":"https://api.github.com/repos/Imhonet-ru/Connection/branches{/branch}","tags_url":"https://api.github.com/repos/Imhonet-ru/Connection/tags","blobs_url":"https://api.github.com/repos/Imhonet-ru/Connection/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/Imhonet-ru/Connection/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/Imhonet-ru/Connection/git/refs{/sha}","trees_url":"https://api.github.com/repos/Imhonet-ru/Connection/git/trees{/sha}","statuses_url":"https://api.github.com/repos/Imhonet-ru/Connection/statuses/{sha}","languages_url":"https://api.github.com/repos/Imhonet-ru/Connection/languages","stargazers_url":"https://api.github.com/repos/Imhonet-ru/Connection/stargazers","contributors_url":"https://api.github.com/repos/Imhonet-ru/Connection/contributors","subscribers_url":"https://api.github.com/repos/Imhonet-ru/Connection/subscribers","subscription_url":"https://api.github.com/repos/Imhonet-ru/Connection/subscription","commits_url":"https://api.github.com/repos/Imhonet-ru/Connection/commits{/sha}","git_commits_url":"https://api.github.com/repos/Imhonet-ru/Connection/git/commits{/sha}","comments_url":"https://api.github.com/repos/Imhonet-ru/Connection/comments{/number}","issue_comment_url":"https://api.github.com/repos/Imhonet-ru/Connection/issues/comments{/number}","contents_url":"https://api.github.com/repos/Imhonet-ru/Connection/contents/{+path}","compare_url":"https://api.github.com/repos/Imhonet-ru/Connection/compare/{base}...{head}","merges_url":"https://api.github.com/repos/Imhonet-ru/Connection/merges","archive_url":"https://api.github.com/repos/Imhonet-ru/Connection/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/Imhonet-ru/Connection/downloads","issues_url":"https://api.github.com/repos/Imhonet-ru/Connection/issues{/number}","pulls_url":"https://api.github.com/repos/Imhonet-ru/Connection/pulls{/number}","milestones_url":"https://api.github.com/repos/Imhonet-ru/Connection/milestones{/number}","notifications_url":"https://api.github.com/repos/Imhonet-ru/Connection/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/Imhonet-ru/Connection/labels{/name}","releases_url":"https://api.github.com/repos/Imhonet-ru/Connection/releases{/id}","deployments_url":"https://api.github.com/repos/Imhonet-ru/Connection/deployments","created_at":"2013-07-17T13:09:49Z","updated_at":"2016-03-10T13:33:02Z","pushed_at":"2016-04-11T14:43:28Z","git_url":"git://github.com/Imhonet-ru/Connection.git","ssh_url":"git@github.com:Imhonet-ru/Connection.git","clone_url":"https://github.com/Imhonet-ru/Connection.git","svn_url":"https://github.com/Imhonet-ru/Connection","homepage":null,"size":225,"stargazers_count":0,"watchers_count":0,"language":"PHP","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":4,"mirror_url":null,"open_issues_count":3,"forks":4,"open_issues":3,"watchers":0,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/Imhonet-ru/Connection/pulls/47"},"html":{"href":"https://github.com/Imhonet-ru/Connection/pull/47"},"issue":{"href":"https://api.github.com/repos/Imhonet-ru/Connection/issues/47"},"comments":{"href":"https://api.github.com/repos/Imhonet-ru/Connection/issues/47/comments"},"review_comments":{"href":"https://api.github.com/repos/Imhonet-ru/Connection/pulls/47/comments"},"review_comment":{"href":"https://api.github.com/repos/Imhonet-ru/Connection/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/Imhonet-ru/Connection/pulls/47/commits"},"statuses":{"href":"https://api.github.com/repos/Imhonet-ru/Connection/statuses/a7269df822ac9909e3a3a58bf1d777c80444e4c0"}}}}