Example one,
// Create a redis instance based on the redis service address and port, the redis port generally defaults to 6379 Jedis jedis = new Jedis("10.110.20.152", 6379); // Set the string value as value of the key. // The string can't be longer than 1073741824 bytes (1 * GB) jedis.set("china", "中国"); // Get the value of the specified key. // If the key does not exist null is returned. // If the value stored at key is not a string an error is returned // because GET can only handle string values. String value = jedis.get("china"); // print result: China System.out.println(value); // Closes this stream and releases any system resources //associated with it. jedis.close();
View through Redis client
TTL:-1 means never expires.
Example two,
// Create a redis instance based on the redis service address and port, the redis port generally defaults to 6379 Jedis jedis = new Jedis("10.110.20.152", 6379); /** * Set the string value as value of the key. * The string can't be longer than 1073741824 bytes (1 * GB). * @param key * @param value * @param nxxx NX | XX, * NX -- Only set the key if it does not already exist. * XX -- Only set the key if it already exist. * @param expx EX|PX, expire time units: EX = seconds; PX = milliseconds * @param time expire time in the units of <code>expx</code> **/ jedis.set("china", "China","NX","EX",100);//It will be deleted automatically after 100 seconds. // Get the value of the specified key. // If the key does not exist null is returned. // If the value stored at key is not a string an error is returned // because GET can only handle string values. String value = jedis.get("china"); // print result: China System.out.println(value); // Closes this stream and releases any system resources //associated with it. jedis.close();
View through Redis client
After the expiration time is reached, the cached information will be automatically deleted.