Various NoSQL databases have the following features:

  1. Do not use relational models
  2. It works well in a cluster
  3. Open source
  4. There is no pattern
  5. Applicable to 21st century Internet companies


Key value database — Redis

Key-value databases are the simplest NoSQL databases. The client can query the value based on the key, set the value corresponding to the key, and delete the key-value pair.

After introducing dependencies into your project, add the Redis configuration information to the application.properties configuration file

Database =0 spring.redis.host= 127.0.0.1 spring.redis.port=6379 spring.redis.password= spring.redis.jedis.pool.max-idle=8 spring.redis.jedis.pool.min-idle=0 spring.redis.jedis.pool.max-active=8 spring.redis.jedis.pool.max-wait=-1ms spring.redis.timeout=30000msCopy the code
  1. To start with the simplest store, create a new RedisTemplate in the Config class that specifies the store key as string and value as object

    @Bean
    public RedisTemplate redisTemplate(RedisConnectionFactory factory){
        RedisTemplate redisTemplate = new RedisTemplate<String,Object>();
        redisTemplate.setConnectionFactory(factory);
        return redisTemplate;
    }Copy the code
  2. Then I Test in Test and you can see that the console prints “value”

    @Test
    public void test() {
       redisTemplate.opsForValue().set("key"."value");
       System.out.println(redisTemplate.opsForValue().get("key"));
    }Copy the code
  3. Next, configure HashOperations in config

    @Bean
    public HashOperations hashOperations(RedisTemplate redisTemplate){
        return  redisTemplate.opsForHash();
    }Copy the code
  4. Create a RedisService that instantiates HashOperations to operate on the hash table stored in the database

    @Service
    public class RedisService {
        @Autowired
        RedisTemplate<String,Object> redisTemplate;
        @Autowired
        HashOperations<String,String,Object> hashOperations;
    
        public void set(String key,String hashKey,Object value){
            hashOperations.put(key,hashKey,value);
        }
    
        public Object get(String key,String hashKey){
            return hashOperations.get(key,hashKey);
        }
    
        public void delete(String key,String hashKey){
            hashOperations.delete(key,hashKey);
        }
    
        public Object getAll(String key){
            return hashOperations.entries(key); }}Copy the code
  5. Create a Controller class to respond to the different actions. After starting the project, type “localhost: 8080/add” and “localhost: 8080/getAll” can see the insertion success prompt and just inserted such as data, open the client with the command line input “HKEYS 1” can also display the fields in 1
        @RequestMapping(value = "/add", method = RequestMethod.GET)
        @ResponseBody
        public void test(){
            String key = "1"; user.setId(key); user.setName("A"); user.setAge("22");
            service.set(user.getId(),"name",user.getName());
            service.set(user.getId(),"age",user.getAge());
    
            key = "2"; user.setId(key); user.setName("B"); user.setAge("23");
            service.set(user.getId(),"name",user.getName());
            service.set(user.getId(),"age",user.getAge());
    
            System.out.println("add success end...");
        }
    
        @RequestMapping(value = "/getAll", method = RequestMethod.GET)
        @ResponseBody
        public Object getAll() {return  service.getAll("1");
        }Copy the code

During the whole process, the default serialization key and value objects were replaced in Config because the default serialization key and value objects were found to be garbled and inconvenient for the client command line to view directly.

redisTemplate.setConnectionFactory(factory);
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setHashValueSerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new StringRedisSerializer());Copy the code

Redis publishes subscriptions

The first step is to initialize the listener in the Config file, configure the adapter to bind the subscription channel, and create the execution method once the message is heard.

@Bean
RedisMessageListenerContainer container(RedisConnectionFactory factory,MessageListenerAdapter adapter){
    RedisMessageListenerContainer container = new RedisMessageListenerContainer();
    container.setConnectionFactory(factory);
    container.addMessageListener(adapter,new PatternTopic("chat"));
    return container;
}

@Bean
MessageListenerAdapter listenerAdapter(RedisReceiver receiver){
    return new MessageListenerAdapter(receiver,"receiveMessage");
}Copy the code

You then create the receiver responsible for receiving the message and specify the actions it needs to perform once it receives the message.

@Component
public class RedisReceiver {
    public void receiveMessage(String message) {
        System.out.println("Received:<"+message+">"); }}Copy the code

Finally, in the Main function, we need to instantiate StringRedisTemplate to publish the message. After the project is started, you can see the Receiver class in the console printing out the corresponding message.

public static void main(String[] args) {
    ApplicationContext context = SpringApplication.run(RedispsApplication.class,args);
    StringRedisTemplate redisTemplate = context.getBean(StringRedisTemplate.class);
    redisTemplate.convertAndSend("chat"."From Redis");
    System.out.println("Send Success");
}Copy the code