本文整理汇总了PHP中apcu_add函数的典型用法代码示例。如果您正苦于以下问题:PHP apcu_add函数的具体用法?PHP apcu_add怎么用?PHP apcu_add使用的例子?那么, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了apcu_add函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: set
public function set(string $key, string $value, int $life = 3600) : bool
{
if ($life == 0) {
$life = null;
}
return apcu_add($key, $value, $life);
}
示例2: cacheData
public function cacheData($key, $data, $timeCache = null)
{
if (apcu_exists($key)) {
return apcu_fetch($key, false);
} else {
if (!empty($timeCache)) {
apcu_add($key, $data, $timeCache);
} else {
apcu_add($key, $data, $this->timeCache);
}
}
}
示例3: set
/**
* {@inheritdoc}
*
* @return \Orno\Cache\Adapter\Apc
*/
public function set($key, $data, $expiry = null)
{
if (is_null($expiry)) {
$expiry = $this->getExpiry();
}
if (is_string($expiry)) {
$expiry = $this->convertExpiryString($expiry);
}
if ($this->apcu) {
apcu_add($key, $data, $expiry);
} else {
apc_add($key, $data, $expiry);
}
return $this;
}
示例4: __construct
public function __construct($namespace = '', $defaultLifetime = 0, $version = null)
{
if (!static::isSupported()) {
throw new CacheException('APCu is not enabled');
}
if ('cli' === PHP_SAPI) {
ini_set('apc.use_request_time', 0);
}
parent::__construct($namespace, $defaultLifetime);
if (null !== $version) {
CacheItem::validateKey($version);
if (!apcu_exists($version . ':' . $namespace)) {
$this->clear($namespace);
apcu_add($version . ':' . $namespace, null);
}
}
}
示例5: addVote
public function addVote(Request $request)
{
$object = $this->getObject($request->get('id'), $request->get('type'));
if (!$object) {
return response()->make('Object not found', 404);
}
if ($this->getVoteElement($object, Auth::user())) {
return response()->make('Already voted', 400);
}
if ($object->user->getKey() == Auth::id()) {
return response()->make('Do not cheat', 400);
}
if ($object instanceof ContentRelated) {
$group = $object->content->group;
} else {
$group = $object->group;
}
if (Auth::user()->isBanned($group)) {
return response()->make('Banned', 400);
}
if (!apcu_add('anti_vote_flood.user.' . Auth::id(), 1, 1)) {
return response()->make('Don\'t flood', 400);
}
$uv = $object->uv;
$dv = $object->dv;
$up = request('up') === 'true';
if ($up) {
$object->increment('uv');
$object->increment('score');
// small hack, as increment function doesn't update object :(
$uv++;
// small trigger, needed for pushing contents to front page
if ($object instanceof Content && !$object->frontpage_at && $object->uv > config('strimoid.homepage.threshold') && $object->created_at->diffInDays() < config('strimoid.homepage.time_limit')) {
$object->frontpage_at = new Carbon();
$object->save();
}
} else {
$object->increment('dv');
$object->decrement('score');
$dv++;
}
$object->votes()->create(['created_at' => new Carbon(), 'user_id' => Auth::id(), 'up' => $up]);
return response()->json(['status' => 'ok', 'uv' => $uv, 'dv' => $dv]);
}
示例6: testApcu
public function testApcu()
{
$key = __CLASS__;
apcu_delete($key);
$this->assertFalse(apcu_exists($key));
$this->assertTrue(apcu_add($key, 123));
$this->assertTrue(apcu_exists($key));
$this->assertSame(array($key => -1), apcu_add(array($key => 123)));
$this->assertSame(123, apcu_fetch($key));
$this->assertTrue(apcu_store($key, 124));
$this->assertSame(124, apcu_fetch($key));
$this->assertSame(125, apcu_inc($key));
$this->assertSame(124, apcu_dec($key));
$this->assertTrue(apcu_cas($key, 124, 123));
$this->assertFalse(apcu_cas($key, 124, 123));
$this->assertTrue(apcu_delete($key));
$this->assertFalse(apcu_delete($key));
$this->assertArrayHasKey('cache_list', apcu_cache_info());
}
示例7: cache_Create
function cache_Create($key, $value = null, $ttl = 0)
{
global $CACHE_STORE_COUNT;
$CACHE_STORE_COUNT++;
return apcu_add($key, $value, $ttl);
}
示例8: add
/**
* {@inheritdoc}
*/
public function add($key, $value, $ttl = 0)
{
$res = apcu_add($key, $value, $ttl);
return $res;
}
示例9: lock
/**
* Lock cached item
*
* @param string $key The cache data key
* @param string $group The cache data group
* @param integer $locktime Cached item max lock time
*
* @return boolean
*
* @since 1.2.7
*/
public function lock($key, $group, $locktime)
{
$output = array();
$output['waited'] = false;
$looptime = $locktime * 10;
$cache_key = $this->_getCacheId($key, $group) . '_lock';
$data_lock = apcu_add($cache_key, 1, $locktime);
if ($data_lock === false) {
$lock_counter = 0;
// Loop until you find that the lock has been released. That implies that data get from other thread has finished
while ($data_lock === false) {
if ($lock_counter > $looptime) {
$output['locked'] = false;
$output['waited'] = true;
break;
}
usleep(100);
$data_lock = apcu_add($cache_key, 1, $locktime);
$lock_counter++;
}
}
$output['locked'] = $data_lock;
return $output;
}
示例10: apcu_add
/**
* @param string|string[] $key
* @param mixed $var
* @param int $ttl
*
* @return bool|bool[]
*/
protected function apcu_add($key, $var, $ttl = 0)
{
if (function_exists('apcu_add')) {
return apcu_add($key, $var, $ttl);
} else {
return apc_add($key, $var, $ttl);
}
}
示例11: withMemory
/**
* Try to cache the callback result in APCU
* @param type $name
* @param \Blend\Component\Cache\callable $callback
* @return mixed
*/
private function withMemory($name, callable $callback)
{
if (apcu_exists($name)) {
$success = false;
$result = apcu_fetch($name, $success);
if ($success) {
return $result;
} else {
$this->logger->warning('APCU cache did not return correctly!', ['cache' => $name]);
return call_user_func($callback);
}
} else {
$result = call_user_func($callback);
if (!apcu_add($name, $result)) {
$this->logger->warning("Unable to store data in cache!", ['data' => $result]);
}
return $result;
}
}
示例12: add
public function add($key, $value, $seconds = 0)
{
$key = $this->prefix . $key;
return $this->apcu ? apcu_add($key, $value, $seconds) : apc_add($key, $value, $seconds);
}
示例13: internalDecrementItem
/**
* Internal method to decrement an item.
*
* @param string $normalizedKey
* @param int $value
* @return int|bool The new value on success, false on failure
* @throws Exception\ExceptionInterface
*/
protected function internalDecrementItem(&$normalizedKey, &$value)
{
$options = $this->getOptions();
$namespace = $options->getNamespace();
$prefix = $namespace === '' ? '' : $namespace . $options->getNamespaceSeparator();
$internalKey = $prefix . $normalizedKey;
$value = (int) $value;
$newValue = apcu_dec($internalKey, $value);
// initial value
if ($newValue === false) {
$ttl = $options->getTtl();
$newValue = -$value;
if (!apcu_add($internalKey, $newValue, $ttl)) {
throw new Exception\RuntimeException("apcu_add('{$internalKey}', {$newValue}, {$ttl}) failed");
}
}
return $newValue;
}
示例14: addValues
/**
* Adds multiple key-value pairs to cache.
* @param array $data array where key corresponds to cache key while value is the value stored
* @param integer $duration the number of seconds in which the cached values will expire. 0 means never expire.
* @return array array of failed keys
*/
protected function addValues($data, $duration)
{
$result = $this->useApcu ? apcu_add($data, null, $duration) : apc_add($data, null, $duration);
return is_array($result) ? array_keys($result) : [];
}
示例15: apc_add
function apc_add($key, $var, $ttl = 0)
{
return apcu_add($key, $var, $ttl);
}