With Memcached, there are requirements for the size of the cached object, a single object should not be larger than 1MB, and does not support complex data types, such as SET
Wait. Based on these limitations, it is necessary to consider Redis!
Related Links:
Redis in action
Redis + Jedis in Redis combat
Redis combat conquest Redis + Jedis + Spring (1)
Redis combat conquest Redis + Jedis + Spring (2)
Redis combat conquest Redis + Jedis + Spring (3)
Closer to home, there are currently about 3 Java-based clients in Redis:
?Jredis
?Jedis
?Redis4J
Only Jedis is mentioned here, because it is the only officially provided Redis Client For Java Provider!
1. Simple use of Jedis
Get it from Maven if you need Jedis!
Maven Pom.xml
Xml代码
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.1.0</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
[xml] view plain copy
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.1.0</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
If you just use Jedis simply, the following lines of code are enough:
Java code
Jedis jedis = new Jedis("10.11.20.140");
String keys = "name";
// delete data
jedis.del(keys);
// save data
jedis.set(keys, "snowolf");
// Get data
String value = jedis.get(keys);
System.out.println(value);
[java] view plain copy
Jedis jedis = new Jedis("10.11.20.140");
String keys = "name";
// delete data
jedis.del(keys);
// save data
jedis.set(keys, "snowolf" );
// Get data
String value = jedis.get(keys);
System.out.println(value);
2. Pooling using Jedis
Jedis uses commons-pool to complete the pooling implementation.
First make a configuration file:
Properties code
#Maximum number of allocated objects
redis.pool.maxActive=1024 #Maximum number of objects that
can maintain the idel state
redis.pool.maxIdle=200 #When
no objects are returned in the pool, the maximum waiting time
redis.pool.maxWait=1000
# When calling the borrow Object method, whether to check the validity
redis.pool.testOnBorrow=true #When
calling the return Object method, whether to check the validity
redis.pool.testOnReturn=true
#IP
redis.ip=
10.11.20.140 #Port
redis.port=6379
The initialization is done in the static code segment:
Java代码
private static JedisPool pool;
static {
ResourceBundle bundle = ResourceBundle.getBundle("redis");
if (bundle == null) {
throw new IllegalArgumentException(
"[redis.properties] is not found!");
}
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxActive(Integer.valueOf(bundle
.getString("redis.pool.maxActive")));
config.setMaxIdle(Integer.valueOf(bundle
.getString("redis.pool.maxIdle")));
config.setMaxWait(Long.valueOf(bundle.getString("redis.pool.maxWait")));
config.setTestOnBorrow(Boolean.valueOf(bundle
.getString("redis.pool.testOnBorrow")));
config.setTestOnReturn(Boolean.valueOf(bundle
.getString("redis.pool.testOnReturn")));
pool = new JedisPool(config, bundle.getString("redis.ip"),
Integer.valueOf(bundle.getString("redis.port")));
}
[java] view plain copy
private static JedisPool pool;
static {
ResourceBundle bundle = ResourceBundle.getBundle("redis");
if (bundle == null) {
throw new IllegalArgumentException(
"[redis.properties] is not found!");
}
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxActive(Integer.valueOf(bundle
.getString("redis.pool.maxActive")));
config.setMaxIdle(Integer.valueOf(bundle
.getString("redis.pool.maxIdle")));
config.setMaxWait(Long.valueOf(bundle.getString("redis.pool.maxWait")));
config.setTestOnBorrow(Boolean.valueOf(bundle
.getString("redis.pool.testOnBorrow")));
config.setTestOnReturn(Boolean.valueOf(bundle
.getString("redis.pool.testOnReturn")));
pool = new JedisPool(config, bundle.getString("redis.ip"),
Integer.valueOf(bundle.getString("redis.port")));
}
Then, modify the previous paragraph of jedis to operate Redis
Java code
// Get a Jedis object from the pool
Jedis jedis = pool.getResource();
String keys = "name";
// delete data
jedis.del(keys);
// save data
jedis.set(keys, "snowolf ");
// Get data
String value = jedis.get(keys);
System.out.println(value);
// Release the object pool
pool.returnResource(jedis);
[java] view plain copy
// Get a Jedis object from the pool
Jedis jedis = pool.getResource();
String keys = "name";
// delete data
jedis.del(keys);
// save data
jedis.set( keys, "snowolf");
// Get data
String value = jedis.get(keys);
System.out.println(value);
// Release the object pool
pool.returnResource(jedis);
Instead, get the Jedis instance from the object pool:
Java code
// Get a Jedis object from the pool
Jedis jedis = pool.getResource();
[java] view plain copy
// Get a Jedis object from the pool
Jedis jedis = pool.getResource();
Remember, after the last use, release the Jedis object:
Java code
// Release the object pool
pool.returnResource(jedis);
[java] view plain copy
// Release the object pool
pool.returnResource(jedis);
3. Consistent hashing
Memcached is completely based on distributed clusters, and Redis is a Master-Slave. If you want to make Reids into a cluster mode, you need to do several sets of Master-Slave. Each set of Master-Slave completes its own disaster recovery processing through the Client tool. , complete consistent hashing.
PS: Memcached completes sharding on the server side, and Redis can only rely on each client for sharding. Server-side Sharding may be supported in the Redis 3.0 series.
Keep the previous JedisPoolConfig, add two Redis IPs (redis1.ip, redis2.ip), complete two JedisShardInfo instances, and throw them into the List:
Java代码
JedisShardInfo jedisShardInfo1 = new JedisShardInfo(
bundle.getString("redis1.ip"), Integer.valueOf(bundle .getString("redis.port")));
JedisShardInfo jedisShardInfo2 = new JedisShardInfo(
bundle.getString("redis2.ip"), Integer.valueOf(bundle .getString("redis.port")));
List<JedisShardInfo> list = new LinkedList<JedisShardInfo>();
list.add(jedisShardInfo1);
list.add(jedisShardInfo2);
[java] view plain copy
JedisShardInfo jedisShardInfo1 = new JedisShardInfo(
bundle.getString("redis1.ip"), Integer.valueOf(bundle .getString("redis.port")));
JedisShardInfo jedisShardInfo2 = new JedisShardInfo(
bundle.getString("redis2.ip"), Integer.valueOf(bundle .getString("redis.port")));
List<JedisShardInfo> list = new LinkedList<JedisShardInfo>();
list.add(jedisShardInfo1);
list.add(jedisShardInfo2);
Initialize ShardedJedisPool instead of JedisPool:
Java代码
ShardedJedisPool pool = new ShardedJedisPool(config, list);
[java] view plain copy
1.ShardedJedisPool pool = new ShardedJedisPool(config, list);
Change to ShardedJedis to get the Jedis object:
Java code
// Get a Jedis object from the pool
ShardedJedis jedis = pool.getResource();
String keys = "name";
String value = "snowolf";
// delete data
jedis.del(keys);
// save data
jedis .set(keys, value);
// Get data
String v = jedis.get(keys);
System.out.println(v);
// Release the object pool
pool.returnResource(jedis);
[java] view plain copy
// Get a Jedis object from the pool
ShardedJedis jedis = pool.getResource();
String keys = "name";
String value = "snowolf";
// delete data
jedis.del(keys);
/ / Save data
jedis.set(keys, value);
// Get data
String v = jedis.get(keys);
System.out.println(v);
// Release the object pool
pool.returnResource(jedis);
Four, Spring package reference
Ok, completing the above code is enough for a simple task, and if necessary, it can be initialized with Spring encapsulation:
Xml代码
<context:property-placeholder location="classpath:redis.properties" />
<bean
id="jedisPoolConfig"
class="redis.clients.jedis.JedisPoolConfig"
>
<property
name="maxActive"
value="${redis.pool.maxActive}" />
<property
name="maxIdle"
value="${redis.pool.maxIdle}" />
<property
name="maxWait"
value="${redis.pool.maxWait}" />
<property
name="testOnBorrow"
value="${redis.pool.testOnBorrow}" />
</bean>
<bean
id="shardedJedisPool"
class="redis.clients.jedis.ShardedJedisPool"
>
<constructor-arg
index="0"
ref="jedisPoolConfig" />
<constructor-arg index="1">
<list>
<bean class="redis.clients.jedis.JedisShardInfo">
<constructor-arg
index="0"
value="${redis1.ip}" />
<constructor-arg
index="1"
value="${redis.port}"
type="int" />
</bean>
<bean class="redis.clients.jedis.JedisShardInfo">
<constructor-arg
index="0"
value="${redis2.ip}" />
<constructor-arg
index="1"
value="${redis.port}"
type="int" />
</bean>
</list>
</constructor-arg>
</bean>
[xml] view plain copy
<context:property-placeholder location="classpath:redis.properties" />
<bean
id="jedisPoolConfig"
class="redis.clients.jedis.JedisPoolConfig"
>
<property
name="maxActive"
value="${redis.pool.maxActive}" />
<property
name="maxIdle"
value="${redis.pool.maxIdle}" />
<property
name="maxWait"
value="${redis.pool.maxWait}" />
<property
name="testOnBorrow"
value="${redis.pool.testOnBorrow}"/>
</bean>
<bean
id="shardedJedisPool"
class="redis.clients.jedis.ShardedJedisPool"
>
<constructor-arg
index="0"
ref="jedisPoolConfig" />
<constructor-arg index="1">
<list>
<bean class="redis.clients.jedis.JedisShardInfo">
<constructor-arg
index="0"
value="${redis1.ip}" />
<constructor-arg
index="1"
value="${redis.port}"
type="int" />
</bean>
<bean class="redis.clients.jedis.JedisShardInfo">
<constructor-arg
index="0"
value="${redis2.ip}" />
<constructor-arg
index="1"
value="${redis.port}"
type="int" />
</bean>
</list>
</constructor-arg>
</bean>
The code could be a bit more concise:
Java code
private ApplicationContext app;
private ShardedJedisPool pool;
@Before
public void before() throws Exception {
app = new ClassPathXmlApplicationContext("applicationContext.xml");
pool = (ShardedJedisPool) app.getBean("shardedJedisPool");
}
@Test
public void test() {
// Get a Jedis object from the pool
ShardedJedis jedis = pool.getResource();
String keys = "name";
String value = "snowolf";
// delete data
jedis.del(keys);
// Save data
jedis.set(keys, value);
// Get data
String v = jedis.get(keys);
System.out.println(v);
// Release the object pool
pool.returnResource(jedis);
assertEquals(value, v);
}
[java] view plain copy
private ApplicationContext app;
private ShardedJedisPool pool;
@Before
public void before() throws Exception {
app = new ClassPathXmlApplicationContext("applicationContext.xml");
pool = (ShardedJedisPool) app.getBean("shardedJedisPool");
}
@Test
public void test() {
// 从池中获取一个Jedis对象
ShardedJedis jedis = pool.getResource();
String keys = "name";
String value = "snowolf";
// 删数据
jedis.del(keys);
// 存数据
jedis.set(keys, value);
// 取数据
String v = jedis.get(keys);
System.out.println(v);
// Release the object pool
pool.returnResource(jedis);
assertEquals(value, v);
}
Original source: