Problem Description
Want to write data to a different dbindex of the same Redis instance in a Java Class, very similar to the [how can select Dbindex if I use Redistemplate in Spring on Stackoverflowe -data-redis?. In this article, we describe how to access redis using spring boot, specifying Dbindex when creating jedisconnectionfactory:
Jedisconnectionfactory factory = new Jedisconnectionfactory ();
...
Factory.setdatabase (databaseId);//set dbindex
Therefore, the approximate idea is to configure 2 redistemplate, one of which redistemplate is responsible for accessing the Dbindex=1 database, and the other redistemplate is responsible for accessing the DBINDEX=3 database.
Based on this article, multiple redistemplate are generated by @Bean (name=). However, since the generation of redistemplate requires an incoming jedisconnectionfactory instance, we specified in Jedisconnectionfactory which database (Dbindex) to access Redis. Therefore, when creating an jedisconnectionfactory instance, use the @Scope (ScopeName = "prototype") annotation so that the Jedis connection factory is no longer a singleton mode. Therefore, there are two jedisconnectionfactory instances, each of which sets a different dbindex through Jedisconnectionfactory.setdatabase (). This approach can be very foolish and can cause serious performance problems.
Below, let's see how it's configured:
@Scope (ScopeName = "prototype")
Public Jedisconnectionfactory jedisconnectionfactory () {
JedisPoolConfig config = getRedisConfig ();
JedisConnectionFactory factory = new JedisConnectionFactory (config);
factory.setUsePool (true);
factory.setHostName (host);
factory.setPort (port);
return factory;
}
Each call to jedisConnectionFactory () returns a new JedisConnectionFactory instance.
Then define 2 redistemplate Bean,jedisconnectionfactory.setdatabase () methods to set different Dbindex
Import Org.slf4j.Logger;
Import Org.slf4j.LoggerFactory;
Import Org.springframework.beans.factory.annotation.Value;
Import Org.springframework.context.annotation.Bean;
Import org.springframework.context.annotation.Configuration;
Import Org.springframework.context.annotation.Scope;
Import Org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
Import Org.springframework.data.redis.core.RedisTemplate;
Import Org.springframework.data.redis.serializer.StringRedisSerializer;
Import Redis.clients.jedis.JedisPoolConfig;
Import Java.util.Map;
/**
- Created by Administrator on 2018/4/9.
*/
@Configuration
public class Loginmacredisconfig {
private static final Logger logger = LoggerFactory.getLogger (LoginMacRedisConfig.class);
@Value ("1")
private int logmacDatabaseId;
@Value ("3")
private int mobmaskDatabaseId;
@Bean
public JedisPoolConfig getRedisConfig () {
JedisPoolConfig config = new JedisPoolConfig ();
config.setMaxIdle (8);
config.setMinIdle (0);
return config;
}
@Scope (scopeName = "prototype")
public JedisConnectionFactory jedisConnectionFactory () {
JedisPoolConfig config = getRedisConfig ();
JedisConnectionFactory factory = new JedisConnectionFactory (config);
factory.setUsePool (true);
factory.setHostName (host);
factory.setPort (port);
return factory;
}
@Bean (name = "login_mac")
public RedisTemplate <String, Map <String, String >> logmacRedisTemplate () {
final RedisTemplate <String, Map <String, String >> template = new RedisTemplate <> ();
JedisConnectionFactory jedisConnectionFactory = jedisConnectionFactory ();
jedisConnectionFactory.setDatabase (logmacDatabaseId);
template.setConnectionFactory (jedisConnectionFactory);
logger.info ("host: {}, port: {}, database: {}", jedisConnectionFactory.getHostName (), jedisConnectionFactory.getPort (), jedisConnectionFactory.getDatabase ());
StringRedisSerializer stringRedisSerializer = new StringRedisSerializer ();
template.setKeySerializer (stringRedisSerializer);
template.setHashKeySerializer (stringRedisSerializer);
template.setHashValueSerializer (stringRedisSerializer);
return template;
}
@Bean (name = "mobile_mask")
public RedisTemplate <String, Map <String, String >> mobileMaskRedisTemplate () {
final RedisTemplate <String, Map <String, String >> template = new RedisTemplate <> ();
JedisConnectionFactory jedisConnectionFactory = jedisConnectionFactory ();
jedisConnectionFactory.setDatabase (mobmaskDatabaseId);
template.setConnectionFactory (jedisConnectionFactory);
logger.info ("host: {}, port: {}, database: {}", jedisConnectionFactory.getHostName (), jedisConnectionFactory.getPort (), jedisConnectionFactory.getDatabase ());
StringRedisSerializer stringRedisSerializer = new StringRedisSerializer ();
template.setKeySerializer (stringRedisSerializer);
template.setHashKeySerializer (stringRedisSerializer);
template.setHashValueSerializer (stringRedisSerializer);
return template;
}
Finally, by writing a service class, you can inject these two redistemplate at the same time, operating different dbindex on the same Redis server.
Import org.springframework.beans.factory.annotation.Autowired;
Import Org.springframework.beans.factory.annotation.Qualifier;
Import org.springframework.data.redis.core.HashOperations;
Import Org.springframework.data.redis.core.RedisTemplate;
Import Org.springframework.stereotype.Service;
Import Java.util.HashMap;
Import Java.util.Map;
/**
-
Created by Administrator in 2018/4/10.br/>*/
@Service
@ AUTOWIRED
Private redistemplate<string, map<string, string>> template1;
@Autowiredbr/> @Qualifier ("Mobile_mask")
public void Write2redis () {
hashoperations<string, String, string> hashoperations = Template1.opsforhash ();
Map<string, string> values = new hashmap<> ();
Values.put ("Dbindex", "1");
Hashoperations.putall ("123", values);
Template2.opsforhash (). Put ("123", "Dbindex", "3");
}
}
Application.java Startup class Br/> @SpringBootApplication
@Autowired
public static void Main (string[] args) {
Springapplication.run (Application.class, args); br/>}
@Override
Redistestservice.write2redis ();
}
}
In the Redistestservice object: There are two instances of Redistemplate:
Two Redistemplate instances encapsulate two jedisconnectionfactory:
Under Debug results:
2018-04-10 20:18:34.754 INFO 13512---[main] c.y.t.c.redis.loginmacredisconfig:host:192.168.107.253, port:6379, Datab Ase:1
2018-04-10 20:19:06.972 INFO 13512---[main] c.y.t.c.redis.loginmacredisconfig:host:192.168.107.253, port:6379, Datab Ase:3
The final review of the Redis results shows that both Dbindex 1 and Dbindex 3 were successfully written to the data.
Redis 192.168.107.253:6379> SELECT 1
Ok
Redis 192.168.107.253:6379[1]> KEYS
1) "123"
Redis 192.168.107.253:6379[1]> Hget 123 Dbindex
"1"
Redis 192.168.107.253:6379[1]> SELECT 3
Ok
Redis 192.168.107.253:6379[3]> KEYS
1) "123"
Redis 192.168.107.253:6379[3]> Hget 123 Dbindex
"3"
Additional br/> in fact, to access different dbindex in the same application, one way is to create Jedis using Jedispool,jedispool and then call the Select method to choose Dbindex. The specific implementation can refer to this article. However, you cannot use Redistemplate's various convenient interfaces to read and write Redis.
@Bean
Jedispool Jedispool = new Jedispool (Jedispoolconfig (), host, Port);
Jedis Jedis = Jedispool.getresource ();
Jedis.select (3);
return jedispool;
}
In fact, it can be said like this: dbindex is selected through the select method of RedisConnectionCommand, but it still has the same problem, and RedisTemplate cannot be used. RedisConnection redisConnection = redisTemplate.getConnectionFactory (). GetConnection ();
Defaultstringredisconnection stringredisconnection = new Defaultstringredisconnection (redisConnection);
Stringredisconnection.select (2);
Stringredisconnection.set ("Test", "test");
Spring Boot uses multiple redistemplate