快速使用
首先到cmd里面去打开这个redis服务
卸载服务:redis-server --service-uninstall
安装服务:redis-server --service-install redis.windows.conf
开启服务:redis-server --service-start
停止服务:redis-server --service-stop
使用第三句 开启计算机服务
redis-cli.exe -h 127.0.0.1 -p 6379
首先先到f盘里面找到解压好的redis文件夹 然后使用windows终端进入 然后输入上面这一句 这句话的意思是 启动redis的服务端 是本机的 6379端口
然后就可以连接redis 和我们的计算机了
关闭服务
关闭后再去查那些数据 因为是存到的是内存里面 所以 这些数据就直接没了
高级使用
上面介绍的是redis的最最基础的入门接下来介绍的是他的高级使用
快速整合springboot
首先我们需要知道 redis的优势 是什么 快 存也快取也快 这样我们就能使用他的这个特点 将一些比较常用的 热点对象存到内存里面去 这样 我们读取的时候就能变得更加方便。 比如 我有一个商品 大家都想买 如果我放到数据库里面去 那么查询数据库有一大堆的目标是因为这个商品 那我存到内存里面去 这样他就能通过访问内存 来取了 大大减少了数据库的压力。 接下来使用一个demo来介绍怎么使用springboot来进行redis的使用
启动器
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
package com.bsos.service.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.ibatis.type.Alias;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import java.io.Serializable;
import java.sql.Date;
/**
* @author Michelle
*/
@AllArgsConstructor
@NoArgsConstructor
@Data
@Component
@Alias("product")
public class Product implements Serializable {
实体类这里一定要注意实现序列化 因为如果要存到内存里面去 是将这个对象序列化
之后在传进去的 所以要让他能序列化
String PR_ID;
String PR_Name;
Date PR_Day;
int PR_Min;
String PR_Mode;
int PR_Style;
String PR_HLight;
}
package com.bsos.service.config;
import com.bsos.service.pojo.Product;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.tomcat.jni.User;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurationSelector;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
/**
* @author Michelle
*/
@Configuration
public class RedisConfig {
这里是redis的配置类 这里面主要是将他自带的那个redistemple进行了重写 因为我
们使用redis传进去的大多数时候是《string,object》类型的 但是原生的redistemple是泛型
所以我们需要重新写一下 这里就写成 string+product类型的 因为我们的key和value也
需要实现序列化 所以我们使用序列化器来对他们进行序列化 因为他默认的序列化器只能
用string类型 所以我们配置一下 这样就能根据我们的需要来进行序列化。
@Bean
public RedisTemplate<String, Product>redisTemplate(RedisConnectionFactory factory){
RedisTemplate<String,Product>template=new RedisTemplate<>();
//关联
template.setConnectionFactory(factory);
//设置key的序列化器
template.setKeySerializer(new StringRedisSerializer());
//设置value的序列化器
template.setValueSerializer(new Jackson2JsonRedisSerializer<>(Product.class));
return template;
}
}
package com.bsos.service.mapper;
import com.bsos.service.pojo.Product;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import javax.annotation.Resource;
import static org.junit.jupiter.api.Assertions.*;
@SpringBootTest
class ProductMapperTest {
@Autowired
private RedisTemplate<String,Product> redisTemplate;
@Autowired
ProductMapper productMapper;
@Test
void delProductById() {
Product product = new Product();
product.setPR_ID("123456");
product.setPR_Name("test");
// redisTemplate.opsForValue().set("myProduct",product);
Product myProduct = redisTemplate.opsForValue().get("myProduct");
System.out.println(myProduct.toString());
System.out.println(redisTemplate.opsForValue().get("myProduct"));
}
这里就是试一下这个起作用了没 很明显他是查到了 而且读取出来的时候也能自动的转化为我们的原来的类型
}