spring boot cache using redis

spring boot redis use 1

Redis:
Redis is completely free open source, BSD comply with the agreement, is a high-performance key-value database.
Redis and other key - value caching product has the following three characteristics:
Redis support persistent data, data in memory can be saved to disk, reboot to load when you can be reused.
Redis only supports simple key-value data types, while also providing a storage list, set, zset, hash and other data structures.
Redis supports backup data, i.e., data backup master-slave mode.

spring boot How to use:

  1. Introducing rely pom.xml

    <!-- redis -->
    <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
    </dependency>
  2. Configuration redis

    #redis configuration
    #Redis server address
    spring.redis.host = 192.168.5.10
    #Redis server port
    spring.redis.port = 6379
    #Redis index database (default 0)
    spring.redis.database. 4 =
    # connection pool maximum number of connections (negative values no limit)
    spring.redis.jedis.pool.max-Active = 50
    # latency connection pool maximum blocking (negative values no limit)
    spring.redis.jedis.pool.max = the wait-3000
    # maximum idle connection pool connection
    spring.redis.jedis.pool.max-iDLE 20 is =
    # minimum connection pool idle connections
    spring.redis.jedis.pool.min-iDLE = 2
    # connection time (ms)
    Spring. redis.timeout = 5000

  3. Modify the startup class notes add @EnableCaching

    @RestControllerbr/>@SpringBootApplication
    @MapperScan("com.example.demo.dao.mybatis")
    br/>@EnableCaching
    public class DemoApplication

  4. Entity classes to add serialization support

    public class User implements Serializable

  5. Cache comment on the service plus

    @Service
    public class MybatisUserService {
    
            @Autowired
            UserMapper userDao;
    
            public int add(User user){
                return   userDao.add(user);
            }
    
            public int update(User user){
                    return   userDao.update(user);
            }
    
            @Cacheable(value = "user-key")
            public User getById(long id){
                    System.out.println("从数据库获取数据");
                    return   userDao.findUserById(id);
            }
    }

---------------- ------------ automatically added to the end of the cache
manually cache:

@Component
public class RedisUtil {

        @Autowired
        private RedisTemplate<String, String> redisTemplate;

        /**
         * 读取缓存
         *
         * @param key
         * @return
         */
        public String get(final String key) {
                return redisTemplate.opsForValue().get(key);
        }

        /**
         * 写入缓存
         */
        public boolean set(final String key, String value) {
                boolean result = false;
                try {
                        redisTemplate.opsForValue().set(key, value);
                        result = true;
                } catch (Exception e) {
                        e.printStackTrace();
                }
                return result;
        }

        /**
         * 更新缓存
         */
        public boolean getAndSet(final String key, String value) {
                boolean result = false;
                try {
                        redisTemplate.opsForValue().getAndSet(key, value);
                        result = true;
                } catch (Exception e) {
                        e.printStackTrace();
                }
                return result;
        }

        /**
         * 删除缓存
         */
        public boolean delete(final String key) {
                boolean result = false;
                try {
                        redisTemplate.delete(key);
                        result = true;
                } catch (Exception e) {
                        e.printStackTrace();
                }
                return result;
        }
}

redis simple operation complete description

Guess you like

Origin blog.51cto.com/5013162/2404306