- function set($id, $data, $group = 'default', $expire = '') {
- if (empty ($group))
- $group = 'default';
-
- if (NULL == $data)
- $data = '';
-
- $this->cache[$group][$id] = $data;
- unset ($this->non_existant_objects[$group][$id]);
- $this->dirty_objects[$group][] = $id;
-
- return true;
- }
-
- function save() {
- //$this->stats();
-
- if (!$this->cache_enabled)
- return true;
-
- if (empty ($this->dirty_objects))
- return true;
-
- // Give the new dirs the same perms as wp-content.
- $stat = stat(ABSPATH.'wp-content');
- $dir_perms = $stat['mode'] & 0007777; // Get the permission bits.
- $file_perms = $dir_perms & 0000666; // Remove execute bits for files.
-
- // Make the base cache dir.
- if (!file_exists($this->cache_dir)) {
- if (! @ mkdir($this->cache_dir))
- return false;
- @ chmod($this->cache_dir, $dir_perms);
- }
-
- if (!file_exists($this->cache_dir."index.php")) {
- @ touch($this->cache_dir."index.php");
- @ chmod($this->cache_dir."index.php", $file_perms);
- }
-
- if ( ! $this->acquire_lock() )
- return false;
-
- // Loop over dirty objects and save them.
- $errors = 0;
- foreach ($this->dirty_objects as $group => $ids) {
- if ( in_array($group, $this->non_persistent_groups) )
- continue;
-
- $group_dir = $this->make_group_dir($group, $dir_perms);
-
- $ids = array_unique($ids);
- foreach ($ids as $id) {
- $cache_file = $group_dir.$this->hash($id).'.php';
-
- // Remove the cache file if the key is not set.
- if (!isset ($this->cache[$group][$id])) {
- if (file_exists($cache_file))
- @ unlink($cache_file);
- continue;
- }
-
- $temp_file = tempnam($group_dir, 'tmp');
- $serial = CACHE_SERIAL_HEADER.base64_encode(serialize($this->cache[$group][$id])).CACHE_SERIAL_FOOTER;
- $fd = @fopen($temp_file, 'w');
- if ( false === $fd ) {
- $errors++;
- continue;
- }
- fputs($fd, $serial);
- fclose($fd);
- if (!@ rename($temp_file, $cache_file)) {
- if (!@ copy($temp_file, $cache_file))
- $errors++;
- @ unlink($temp_file);
- }
- @ chmod($cache_file, $file_perms);
- }