/* wrapper of set command */ static int memc_inc(memcached_st *memc, char *key, uint32_t offset, uint64_t *value) { memcached_return_t rc; rc = memcached_increment(memc, key, key_len, offset, value); if (rc != MEMCACHED_SUCCESS) { return 1; } return 0; }
/** * Increment the value of the object associated with the specified * key by the offset given. The resulting value is saved in the value * parameter. * * @param[in] key key of object in server whose value to increment * @param[in] offset amount to increment object's value by * @param[out] value store the result of the increment here * @return true on success; false otherwise */ bool increment(const std::string &key, uint32_t offset, uint64_t *value) { if (key.empty()) { return false; } memcached_return rc= memcached_increment(&memc, key.c_str(), key.length(), offset, value); return (rc == MEMCACHED_SUCCESS); }
int vmod_incr(struct sess *sp, struct vmod_priv *priv, const char *key, int offset) { uint64_t value = 0; memcached_return rc; memcached_st *mc = get_memcached(priv->priv); if (!mc) return 0; rc = memcached_increment(mc, key, strlen(key), offset, &value); return (int)value; }
static int exec_decr(memcached_st *mcd, const char *key) { memcached_return_t error; uint64_t value; error = memcached_increment(mcd, key, strlen(key), 0, &value); if (error == MEMCACHED_SUCCESS) printf("decr: key=%s value=%llu\n", key, value); else printf("decr: key=%s error=%s\n", key, memcached_strerror(mcd, error)); return 0; }
int64_t c_Memcache::t_increment(CStrRef key, int offset /*= 1*/) { if (key.empty()) { raise_warning("Key cannot be empty"); return false; } uint64_t value; memcached_return_t ret = memcached_increment(&m_memcache, key.c_str(), key.length(), offset, &value); if (ret == MEMCACHED_SUCCESS) { return (int64_t)value; } return false; }
static test_return_t udp_incr_test(memcached_st *memc) { memcached_return_t rc; const char *key= "incr"; const char *value= "1"; rc= memcached_set(memc, key, strlen(key), value, strlen(value), (time_t)0, (uint32_t)0); test_true(rc == MEMCACHED_SUCCESS); uint16_t *expected_ids= get_udp_request_ids(memc); unsigned int server_key= memcached_generate_hash(memc, key, strlen(key)); increment_request_id(&expected_ids[server_key]); uint64_t newvalue; rc= memcached_increment(memc, key, strlen(key), 1, &newvalue); test_true(rc == MEMCACHED_SUCCESS); return post_udp_op_check(memc, expected_ids); }
/* TODO - once memcached_touch gets into libmemcached, also take care of expires */ int wrap_memcached_add(cachedb_con *connection,str* attr,int val, int expires,int *new_val) { memcached_return_t rc; memcached_con *con; uint64_t res; str ins_val; struct timeval start; start_expire_timer(start,memcache_exec_threshold); con = (memcached_con *)connection->data; rc = memcached_increment(con->memc,attr->s,attr->len,val,&res); if( rc != MEMCACHED_SUCCESS ) { if (rc == MEMCACHED_NOTFOUND) { ins_val.s = sint2str(val,&ins_val.len); if (wrap_memcached_insert(connection,attr,&ins_val,expires) < 0) { LM_ERR("failed to insert value\n"); stop_expire_timer(start,memcache_exec_threshold, "cachedb_memcached add",attr->s,attr->len,0); return -1; } if (new_val) *new_val = val; stop_expire_timer(start,memcache_exec_threshold, "cachedb_memcached add",attr->s,attr->len,0); return 0; } else { LM_ERR("Failed to add: %s\n",memcached_strerror(con->memc,rc)); stop_expire_timer(start,memcache_exec_threshold, "cachedb_memcached add",attr->s,attr->len,0); return -1; } } if (new_val) *new_val = (int)res; stop_expire_timer(start,memcache_exec_threshold, "cachedb_memcached add",attr->s,attr->len,0); return 0; }
/*doc Memcached incr([offset]) Asks memcached to increment data for some item in place. The data for the item is treated as decimal representation of a 64-bit unsigned integer. If the current data value does not conform to such a representation, the commands behave as if the value were 0. Default offset is 1. Returns the new value. */ IoObject *IoMemcached_incr(IoMemcached *self, IoObject *locals, IoMessage *m) { IoSeq *key = IoMessage_locals_seqArgAt_(m, locals, 0); uint32_t offset = IoMessage_argCount(m) == 2 ? IoMessage_locals_intArgAt_(m, locals, 1) : 1; uint64_t new_value; memcached_return_t rc; rc = memcached_increment(DATA(self)->mc, CSTRING(key), IOSEQ_LENGTH(key), offset, &new_value ); if(rc != MEMCACHED_SUCCESS) IoState_error_(IOSTATE, m, memcached_strerror(DATA(self)->mc, rc)); return IONUMBER(new_value); }
VALUE mc_incr(int argc, VALUE *argv, VALUE self) { memcached_st *mc; VALUE key, amount; static memcached_return_t result; unsigned int offset; uint64_t value; Data_Get_Struct(self, memcached_st, mc); rb_scan_args(argc, argv, "11", &key, &amount); key = StringValue(key); if (!use_binary(mc)) key = escape_key(key, NULL); offset = RTEST(amount) ? NUM2INT(amount) : 1; result = memcached_increment(mc, RSTRING_PTR(key), RSTRING_LEN(key), offset, &value); if (result == MEMCACHED_SUCCESS) { return LONG2NUM(value); } else if (result == MEMCACHED_NOTFOUND) { return Qnil; } else { return throw_error(&result); } }
int pr_memcache_kincr(pr_memcache_t *mcache, module *m, const char *key, size_t keysz, uint32_t incr, uint64_t *value) { memcached_return res; if (mcache == NULL || m == NULL || key == NULL || incr == 0) { errno = EINVAL; return -1; } /* Note: libmemcached automatically handles the case where value might be * NULL. */ mcache_set_module_namespace(mcache, m); res = memcached_increment(mcache->mc, key, keysz, incr, value); mcache_set_module_namespace(mcache, NULL); if (res == MEMCACHED_NOTFOUND) { /* Automatically create a value for this key, with the given increment. */ pr_trace_msg(trace_channel, 18, "unable to increment nonexistent key (%lu bytes), automatically " "creating one", (unsigned long) keysz); return pr_memcache_kset(mcache, m, key, keysz, &incr, sizeof(uint32_t), 0, 0); } switch (res) { case MEMCACHED_SUCCESS: return 0; case MEMCACHED_ERRNO: if (errno != EINPROGRESS) { int xerrno = errno; pr_trace_msg(trace_channel, 3, "error incrementing key (%lu bytes) by %lu: system error: %s", (unsigned long) keysz, (unsigned long) incr, strerror(xerrno)); errno = xerrno; } else { /* We know that we're not using nonblocking IO; this value usually * means that libmemcached could not connect to the configured * memcached servers. So set the value to something more * indicative, and fall through. */ res = MEMCACHED_CONNECTION_FAILURE; } break; case MEMCACHED_SERVER_MARKED_DEAD: case MEMCACHED_CONNECTION_FAILURE: { memcached_server_instance_st server; server = memcached_server_get_last_disconnect(mcache->mc); if (server != NULL) { pr_trace_msg(trace_channel, 3, "unable to connect to %s:%d", memcached_server_name(server), memcached_server_port(server)); } break; } default: pr_trace_msg(trace_channel, 2, "error incrementing key (%lu bytes) by %lu: %s", (unsigned long) keysz, (unsigned long) incr, memcached_strerror(mcache->mc, res)); errno = EPERM; break; } return -1; }
virtual void execute(memcached_st* mem) { this->debug && printf("%s %s (%zu): %d\n", "IncrementJob execute", key.c_str(), key.size(), delta); rc = memcached_increment(mem, key.c_str(), key.size(), delta, &finalValue); }