本文整理汇总了Java中redis.clients.jedis.Jedis.set方法的典型用法代码示例。如果您正苦于以下问题:Java Jedis.set方法的具体用法?Java Jedis.set怎么用?Java Jedis.set使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类redis.clients.jedis.Jedis
的用法示例。
在下文中一共展示了Jedis.set方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: checkJedisIsReusedWhenReturned
import redis.clients.jedis.Jedis; //导入方法依赖的package包/类
@Test
public void checkJedisIsReusedWhenReturned() {
JedisPool pool = new JedisPool(new JedisPoolConfig(), hnp.getHost(), hnp.getPort());
Jedis jedis = pool.getResource();
jedis.auth("foobared");
jedis.set("foo", "0");
pool.returnResource(jedis);
jedis = pool.getResource();
jedis.auth("foobared");
jedis.incr("foo");
pool.returnResource(jedis);
pool.destroy();
assertTrue(pool.isClosed());
}
示例2: select
import redis.clients.jedis.Jedis; //导入方法依赖的package包/类
@Test
public void select() {
jedis.select(1);
jedis.set("foo", "bar");
jedis.watch("foo");
Transaction t = jedis.multi();
t.select(0);
t.set("bar", "foo");
Jedis jedis2 = createJedis();
jedis2.select(1);
jedis2.set("foo", "bar2");
List<Object> results = t.exec();
if(results.isEmpty()){
results = null;
}
assertNull(results);
}
示例3: set
import redis.clients.jedis.Jedis; //导入方法依赖的package包/类
public boolean set(byte[] key, byte[] value, int validSeconds){
Jedis jedis = null;
try{
jedis = get();
if (jedis == null)
return false;
jedis.set(key,value);
jedis.expire(key, validSeconds);
pool.returnResource(jedis);
}catch(Exception e){
pool.returnBrokenResource(jedis);
return false;
}
return true;
}
示例4: main
import redis.clients.jedis.Jedis; //导入方法依赖的package包/类
public static void main(String[] args) throws UnknownHostException, IOException {
Jedis jedis = new Jedis(hnp.getHost(), hnp.getPort());
jedis.connect();
jedis.auth("foobared");
jedis.flushAll();
long begin = Calendar.getInstance().getTimeInMillis();
for (int n = 0; n <= TOTAL_OPERATIONS; n++) {
String key = "foo" + n;
jedis.set(key, "bar" + n);
jedis.get(key);
}
long elapsed = Calendar.getInstance().getTimeInMillis() - begin;
jedis.disconnect();
System.out.println(((1000 * 2 * TOTAL_OPERATIONS) / elapsed) + " ops");
}
示例5: set
import redis.clients.jedis.Jedis; //导入方法依赖的package包/类
/**
* 简单的set
* @param key
* @param value
*/
public void set(String key ,Object value) throws Exception {
Jedis jds = null;
try {
jds = getJedis();
jds.select(0);
byte[] svalue = SerializeUtil.serialize(value);
jds.set(key.getBytes(), svalue);
} catch (Exception e) {
throw e;
} finally {
if(jds != null)
{
jds.close();
}
}
}
示例6: main
import redis.clients.jedis.Jedis; //导入方法依赖的package包/类
public static void main(String[] args) throws InterruptedException {
Bench<JedisPool> bench = new JedisBench() {
@Override
public void executeOperation(String data, JedisPool benchInstance, int threadNumber, int iteration,
MetricRegistry metrics) {
Jedis jedis = benchInstance.getResource();
Timer.Context time = metrics.timer("bucket").time();
String key = "bucket_" + threadNumber + "_" + iteration;
jedis.set(key, (String) value);
time.stop();
jedis.close();
}
};
Benchmark benchmark = new Benchmark(bench);
benchmark.run(args);
}
示例7: set
import redis.clients.jedis.Jedis; //导入方法依赖的package包/类
/**
* String set command
*
* @param key
* @param value
* @param nxxx
* @param expx
* @param time
* @return
*/
public String set(String key, String value, String nxxx, String expx, long time) {
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
return jedis.set(key, value, nxxx, expx, time);
} finally {
if (jedis != null) {
jedis.close();
}
}
}
示例8: available
import redis.clients.jedis.Jedis; //导入方法依赖的package包/类
public boolean available(){
Jedis jedis = getJedis();
if(jedis!=null){
jedis.select(0);
String key = "[email protected]";
jedis.set(key,"available");
if("available".equals(jedis.get(key))){
jedis.del(key);
return true;
}
}else{
return false;
}
return false;
}
示例9: doTest
import redis.clients.jedis.Jedis; //导入方法依赖的package包/类
private static void doTest() throws Exception {
GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
poolConfig.setMinIdle(0);
poolConfig.setMaxIdle(5);
poolConfig.setMaxTotal(5);
final JedisPool pool = new JedisPool(poolConfig, "127.0.0.1", 6379, 2000, "weimob123", 8);
List<Thread> tds = new ArrayList<Thread>();
final Integer temp = 0;
final AtomicInteger ind = new AtomicInteger();
for (int i = 0; i < 50; i++) {
Thread hj = new Thread(new Runnable() {
public void run() {
for (int i; (i = ind.getAndIncrement()) < TOTAL_OPERATIONS; ) {
Jedis j = pool.getResource();
final String key = "foo" + i;
// redisUtil.setRedisData(key, key);
j.set(key.getBytes(), ConvertUtil.serialize(temp));
j.close();
}
}
});
tds.add(hj);
hj.start();
}
for (Thread t : tds)
t.join();
pool.close();
pool.destroy();
}
示例10: set
import redis.clients.jedis.Jedis; //导入方法依赖的package包/类
/**
* 设置 byte[] 过期时间
* @param key
* @param value
* @param seconds 以秒为单位
*/
public synchronized static void set(byte[] key, byte[] value, int seconds) {
try {
Jedis jedis = getJedis();
jedis.set(key, value);
jedis.expire(key, seconds);
jedis.close();
} catch (Exception e) {
LOGGER.error("Set key error : " + e);
}
}
示例11: set
import redis.clients.jedis.Jedis; //导入方法依赖的package包/类
/**
* 设置 byte[]
* @param key
* @param value
*/
public synchronized static void set(byte[] key, byte[] value) {
try {
Jedis jedis = getJedis();
jedis.set(key, value);
jedis.close();
} catch (Exception e) {
_log.error("Set key error : " + e);
}
}
示例12: lockQueue
import redis.clients.jedis.Jedis; //导入方法依赖的package包/类
private boolean lockQueue(String queueID) {
if (locked.get() == null) {
synchronized (this) {
if (locked.get() == null) {
locked.set(new AtomicInteger(0));
}
}
}
if (locked.get().incrementAndGet() > 1) {
return true;
}
Jedis jedis = jedisPool.getResource();
try {
String redisLockKey = makeRedisLockKey(queueID);
long lockRequestTime = System.currentTimeMillis();
while (true) {
String result = jedis.set(redisLockKey, "lockTheQueue", "NX", "EX", 120);
if (StringUtils.isNotEmpty(result) && result.equalsIgnoreCase("OK")) {
return true;
}
if (lockRequestTime + lockWaitTimeStamp > System.currentTimeMillis()) {
locked.get().decrementAndGet();
return false;
}
long sleepTime = jedis.ttl(redisLockKey) * 1000 - 10;
if (sleepTime > lockRequestTime) {
return false;
}
if (sleepTime > 0) {
CommonUtil.sleep(sleepTime / 4);
}
}
} finally {
IOUtils.closeQuietly(jedis);
}
}
示例13: startWithUrlString
import redis.clients.jedis.Jedis; //导入方法依赖的package包/类
@Test
public void startWithUrlString() {
Jedis j = new Jedis("localhost", 6380);
j.auth("foobared");
j.select(2);
j.set("foo", "bar");
JedisPool pool = new JedisPool("redis://:[email protected]:6380/2");
Jedis jedis = pool.getResource();
assertEquals("PONG", jedis.ping());
assertEquals("bar", jedis.get("foo"));
}
示例14: set
import redis.clients.jedis.Jedis; //导入方法依赖的package包/类
public void set(byte[] key, byte[] value ) {
Jedis jedis = null;
try {
jedis = get();
if (jedis == null)
return;
jedis.set(key, value);
pool.returnResource(jedis);
} catch (Exception e) {
pool.returnBrokenResource(jedis);
}
}
示例15: lockQueue
import redis.clients.jedis.Jedis; //导入方法依赖的package包/类
private boolean lockQueue(String queueID) {
if (locked.get() == null) {
synchronized (this) {
if (locked.get() == null) {
locked.set(new AtomicInteger(0));
}
}
}
boolean ret = false;
@Cleanup Jedis jedis = jedisPool.getResource();
if (locked.get().incrementAndGet() > 1) {
return true;
}
String redisLockKey = makeRedisLockKey(queueID);
long lockRequestTime = System.currentTimeMillis();
while (true) {
String result = jedis.set(redisLockKey, "lockTheQueue", "NX", "EX", 120);
if (StringUtils.isNotEmpty(result) && result.equalsIgnoreCase("OK")) {
ret = true;
break;
}
if (lockRequestTime + lockWaitTimeStamp < System.currentTimeMillis()) {
locked.get().decrementAndGet();
break;
}
long sleepTime = jedis.ttl(redisLockKey) * 1000 - 10;
if (sleepTime > lockRequestTime) {
return false;
}
if (sleepTime > 0) {
CommonUtil.sleep(sleepTime);
}
}
return ret;
}