redisAPI

检查redis服务端配置

  • 检查是否配置redis服务器密码
    在这里插入图片描述
  • 启动服务端
    在这里插入图片描述
  • 检查redi服务端是否允许API程序所运行服务器ip连接或者允许所有ip连接(*代表所有)
    在这里插入图片描述
Jedis操作Redis

  • 1 . 依赖
<!--
            jedis 坐标添加
        -->
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>2.9.0</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
  • 2 . API
package com.credi.xmjf.server.service;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import redis.clients.jedis.Jedis;

/**
 * Created by Administrator on 2019/6/24.
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:spring.xml"})
public class JedisTest {
    @Test
    public void test01(){
        //创建Jedis对象 , 传入构造参数为: redis服务器ip和访问端口
        Jedis jedis = new Jedis("192.168.200.210", 6379);
        //设置redis服务器的密码
        jedis.auth("123456");

        //操作redis
        jedis.set("color","blue");
        jedis.expire("color",90);
        System.out.println(jedis.ttl("color"));
    }
}

Spring Data Redis 操作redis


  • 1 . 依赖
<!--
 jedis 坐标添加
 -->
 <dependency>
     <groupId>redis.clients</groupId>
     <artifactId>jedis</artifactId>
     <version>2.9.0</version>
     <type>jar</type>
     <scope>compile</scope>
 </dependency>
 <!--
    spring-data-redis 坐标添加
 -->
 <dependency>
     <groupId>org.springframework.data</groupId>
     <artifactId>spring-data-redis</artifactId>
     <version>1.8.1.RELEASE</version>
 </dependency>
  • 2 . spring.xml配置
<?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.xsd">
    <!--
            redis 环境配置
        -->

    <!-- 连接池配置 -->
    <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
        <!-- 最大连接数  -->
        <property name="maxTotal" value="1024" />
        <!-- 最大 空闲连接数 -->
        <property name="maxIdle" value="200" />
        <!-- 获取连接时最大等待毫秒数 -->
        <property name="maxWaitMillis" value="10000" />
        <!-- 在获取连接时检查有效性 -->
        <property name="testOnBorrow" value="true" />
    </bean>

    <!--
      jedis 连接工厂配置 : 服务器ip ,端口,密码
        使用了spring的简化p标签 , 注意配置xmlns:p="http://www.springframework.org/schema/p"
    -->
    <bean id="jedisConnFactory"
          class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
          p:use-pool="true" p:hostName="192.168.200.210" p:port="6379" p:password="123456" p:poolConfig-ref="jedisPoolConfig"/>

    <!--
       redis 模板类配置
    -->
    <bean id="redisTemplate"  class="org.springframework.data.redis.core.RedisTemplate">
        <property name="connectionFactory" ref="jedisConnFactory"></property>
        <!--对key和value的序列化-->
        <property name="keySerializer" >
            <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"></bean>
        </property>
        <property name="valueSerializer" >
            <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"></bean>
        </property>
    </bean>
</beans>
  • 3 . API
package com.credi.xmjf.server.service;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * Created by Administrator on 2019/6/24.
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:spring.xml"})
public class SpringDataRedisTest {

    //1 . 注入redis模板对象
    @Resource
    private RedisTemplate<String,Object> redisTemplate;

    @Test
    public void test01(){
        /*
            redisTemplate.opsForValue();//操作字符串
            redisTemplate.opsForHash();//操作hash
            redisTemplate.opsForList();//操作list
            redisTemplate.opsForSet();//操作set
            redisTemplate.opsForZSet();//操作有序set
         */
        // 储存
        redisTemplate.opsForValue().set("today","2019-06-24");
        // 设置有效时间
        redisTemplate.expire("today",90, TimeUnit.SECONDS);
        // 获取
        System.out.println(redisTemplate.opsForValue().get("today"));

    }
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值