redis入门篇之windows下安装redis
windows安装过程
windows版本下载
解压后如下
cmd cd到你的redis目录下,输入redis-server.exe 出现redis 吉祥物图片就说明安装成功了
可以运行另一个cmd窗口,输入redis-cli.exe ,set key lry,再get key,输出lry
jedis使用
添加依赖
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.10.0-m1</version>
</dependency>
代码如下
@Test
public void redisTester() {
Jedis jedis = new Jedis("localhost", 6379, 100000);
jedis.set("key","value");
System.out.println(jedis.get("key"));
}
spring集成
spring-redis配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<!--最大空闲数-->
<property name="maxIdle" value="50"/>
<!--最大连接数-->
<property name="maxTotal" value="100"/>
<!--最大等待时间-->
<property name="maxWaitMillis" value="20000"/>
</bean>
<bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<!--Redis服务地址-->
<property name="hostName" value="localhost"/>
<!--端口号-->
<property name="port" value="6379"/>
<!--如果有密码则需要配置密码-->
<!--<property name="password" value="password"/>-->
<!--连接池配置-->
<property name="poolConfig" ref="poolConfig"/>
</bean>
<bean id="redisTemplate"
class="org.springframework.data.redis.core.RedisTemplate"
p:connection-factory-ref="connectionFactory"/>
</beans>
依赖如下
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.7.2.RELEASE</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.10.0-m1</version>
</dependency>
代码如下
@Test
public void test() {
ApplicationContext context =
new ClassPathXmlApplicationContext("spring-redis.xml");
RedisTemplate redisTemplate = context.getBean(RedisTemplate.class);
UserEntity user = new UserEntity();
user.setId(1);
user.setUsername("lry");
user.setPassword("123");
System.out.println(user);
redisTemplate.opsForValue().set("user", user);
UserEntity user1 = (UserEntity) redisTemplate.opsForValue().get("user");
System.out.println(user1);
}