如何使用redis进行存放数据?

基于redis使用工具类向缓存中加入数据

Posted by MasterJen on November 15, 2018

Hey Redis

A bold attempt is half success. – 勇敢的尝试是成功的一半.

使用redis缓存的几个常用的语句

给一个对象的所有属性进行赋值 
    hset hashname field value
对一个对象进行取值
    hget hashname  field
批量设值
    hmset hashname field1 value1 field2 value2 
批量取值
    hmget hashname field1 field2
返回对象的所有的属性和值
    hgetall key
向set 集合里面进行添加值,保证每个值唯一,一般用来防穿透
    sadd key member [member...]
取元素
    smembers key
删除元素   
    srem key member [member...]   
set 中的差集运算 返回在 第一个set中存在,在第二个set中不存在的元素
    sdiff key [key ...]
判断某元素存不存在该集合中 一般用于防穿刺
 sismember key member     

如何使用java代码实现向缓存中加入数据呢? 基于springboot实现redis

导入依赖包

       <dependency>
                <groupId>redis.clients</groupId>
                <artifactId>jedis</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.data</groupId>
                <artifactId>spring-data-redis</artifactId>
       </dependency>               

配置文件

spring:
     redis:
        port: 8400 #端口号
        host:  #地址
        password:  #密码
        jedis:
          pool:
            min-idle: 10
            max-active: 1024
            max-idle: 100    

配置类 创建jedispool 缓存池

@Configuration
public class RedisJedisPoolConfig {
//    配置 地址
    @Value(("${spring.redis.host}"))
    private String host;
//    端口号
    @Value(("${spring.redis.port}"))
    private int port;
//    密码
    @Value(("${spring.redis.password}"))
    private String password;

    /**
     *  用来创建 jedispool 的配置类  
     * @return  JedisPoolConfig
     */
    @Bean
    public JedisPoolConfig jedisPoolConfig(){
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setMaxTotal(1024);
        jedisPoolConfig.setMinIdle(10);
        jedisPoolConfig.setMaxIdle(100);
        return jedisPoolConfig;
    }

    /**
     *  返回 jedispool 
     * @param jedisPoolConfig
     * @return
     */
    @Bean
    public JedisPool jedisPool(JedisPoolConfig jedisPoolConfig){
        JedisPool jedisPool = new JedisPool(jedisPoolConfig,host ,port ,5000 ,password);
        return jedisPool;
    }
}

创建 Redis工具类 把对象及其属性与值 放入到缓存中

/*
*   处理缓存的  工具  当数据中有新的 数据产生时  需要把新的数据放到缓存中
* */
public class RedisJedisUtils {
    /**
     *   向redis中添加 对象以及属性值
     * @param key 在redis中的
     * @param object 键
     * @param jedis 当前连接
     */
    public static void hSetObject(String key, Object object, Jedis jedis){
//    根据类  进行反射
        Class<?> aClass = object.getClass();
//        得到所有的属性
        Field[] declaredFields = aClass.getDeclaredFields();
//        遍历属性
        for (Field declaredField : declaredFields) {
//            得到属性名
            String name = declaredField.getName();
//            根据用户名进行反射得到里面的值   参数  属性名称 和 类
            try {
                PropertyDescriptor propertyDescriptor = new PropertyDescriptor(name,aClass);
//                得到 类方法  read方法
                Method readMethod = propertyDescriptor.getReadMethod();
//                得到值 根据对象
                  Object invoke = readMethod.invoke(object);
//                  判断值
                if(invoke!=null){
//                    放到缓存中  是  String类型
                    jedis.hset(key,name,invoke.toString());
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

}

测试类

      @Test
        public void test5() {
    //        测试类
    //        创建对象
            User user = new User();
            user.setMobile("1335372453");
            user.setName("张三");
            user.setPassword("123123");
    //        创建jedis
            JedisPool jedisPool = new JedisPool();
            Jedis jedis = jedisPool.getResource();
    //        把对象加入到 redis中 使用工具类  注意 key 可以起个长的名称防止 数据覆盖
            RedisJedisUtils.hSetObject("ceshi",user ,jedis);        
        }

redis基本使用就完成了.