function ad_cache_file_increment in Advertisement 6
Same name and namespace in other branches
- 5.2 cache/file/ad_cache_file.inc \ad_cache_file_increment()
- 5 cache/file/ad_cache_file.inc \ad_cache_file_increment()
- 6.3 cache/file/ad_cache_file.inc \ad_cache_file_increment()
- 6.2 cache/file/ad_cache_file.inc \ad_cache_file_increment()
- 7 cache/file/ad_cache_file.inc \ad_cache_file_increment()
File
- cache/
file/ ad_cache_file.inc, line 309 - A plug in for the ad.module, providing a file cache mechanism for improved performance when displaying ads.
Code
function ad_cache_file_increment($action = 'view', $aid) {
_debug_echo("File cache: incrementing '{$action}'.");
$cache_file = ad_cache_file_get_lock();
if ($cache_file) {
$cache = unserialize(fread(adserve_variable('fd'), filesize($cache_file)));
$hostid = adserve_variable('hostid') ? adserve_variable('hostid') : 'none';
$aid = adserve_variable('aid');
if ($action == 'view') {
if ($hostid != 'none' && !isset($cache['hostid'][$hostid])) {
_debug_echo("File cache: invalid hostid: {$hostid}");
$output = 'You do not have permission to display images.';
// TODO: We should still log this. Perhaps a new $action type?
return -1;
}
}
$last_sync = $cache['last_sync'];
$lifetime = $cache['lifetime'];
$time = time();
$timestamp = date('YmdH');
// Increment action counter.
if (isset($cache['ad'][$aid][$hostid]) && isset($cache['ad'][$aid][$hostid]['counts'][$action]) && isset($cache['ad'][$aid][$hostid]['counts'][$action][$timestamp])) {
$cache['ad'][$aid][$hostid]['counts'][$action][$timestamp]++;
}
else {
$cache['ad'][$aid][$hostid]['counts'][$action][$timestamp] = 1;
}
// Write updated cache back to file and release the lock.
$cache = serialize($cache);
rewind(adserve_variable('fd'));
ftruncate(adserve_variable('fd'), 0);
fwrite(adserve_variable('fd'), $cache, strlen($cache));
flock(adserve_variable('fd'), LOCK_UN);
fclose(adserve_variable('fd'));
adserve_variable('fd', '');
// Every $lifetime seconds we flush the cache files to the database.
if ($last_sync < time() - $lifetime) {
ad_cache_file_rebuild();
}
return 1;
}
return 0;
}