Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

comparing arrays

  • Loading branch information...
commit eb3b75b8402879ce9ac06e91a7f5e200e0b890a5 1 parent 70409f5
Mario Estrada authored
6 app/framework/Cr/Cache/Abstract.php
@@ -41,8 +41,10 @@ protected function _addToTags($name, $tags)
41 41 if(!is_string($tag))
42 42 throw new Exception('Each individual tag needs to be a string.');
43 43
44   - $tags_array[$tag][] = $name;
45   - $tags_array[$tag] = array_unique($tags_array[$tag]);
  44 + if(!isset($tags_array[$tag]) || (is_array($tags_array[$tag]) && !in_array($name, $tags_array[$tag])))
  45 + {
  46 + $tags_array[$tag][] = $name;
  47 + }
46 48 }
47 49
48 50 return $tags_array;
38 app/framework/Cr/Cache/File.php
@@ -88,28 +88,46 @@ public function delete($name)
88 88
89 89 public function addToTags($name, $tags)
90 90 {
91   - $this->_openTagsFile();
92   -
93   - $filename = $this->getFileName($this->tags_key);
94   - $tags_array = $this->_addToTags($name, $tags);
95   -
96   - $this->writeToFile(array('content' => $tags_array), $filename, true);
97   -
98   - $this->_closeTagsFile();
  91 + $this->_modifyTags($name, $tags, 'add');
99 92 }
100 93
101 94 public function deleteByTag($tag)
102 95 {
  96 + $this->_modifyTags('', $tag, 'delete');
  97 + }
  98 +
  99 + private function _modifyTags($name, $tags, $type)
  100 + {
  101 + $tags_old = $this->get($this->tags_key);
  102 + $tags_old = !is_array($tags_old) ? array() : $tags_old;
  103 +
103 104 $this->_openTagsFile();
104 105
105 106 $filename = $this->getFileName($this->tags_key);
106   - $tags_array = $this->_deleteByTag($tag);
107 107
108   - $this->writeToFile(array('content' => $tags_array), $filename, true);
  108 + switch($type)
  109 + {
  110 + case 'delete':
  111 + $tags_array = $this->_deleteByTag($tags);
  112 + break;
  113 +
  114 + case 'add':
  115 + default:
  116 + $tags_array = $this->_addToTags($name, $tags);
  117 + }
  118 +
  119 + $tags_diff = $this->_compare_array($tags_array, $tags_old);
  120 + if(!empty($tags_diff) > 0)
  121 + $this->writeToFile(array('content' => $tags_array), $filename, true);
109 122
110 123 $this->_closeTagsFile();
111 124 }
112 125
  126 + private function _compare_array($a, $b)
  127 + {
  128 + return array_diff($a, $b) + array_diff($b, $a);
  129 + }
  130 +
113 131 private function _openTagsFile()
114 132 {
115 133 if(isset($this->tags_file))

0 comments on commit eb3b75b

Please sign in to comment.
Something went wrong with that request. Please try again.