CRM项目使用redis作为缓存工具类并通过Redis-Service的逻辑拆分实现解耦合------CRM项目

package com.alatus.service.impl;

import com.alatus.service.RedisService;
import jakarta.annotation.Resource;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Collection;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Component
public class RedisServiceImpl implements RedisService {
//    数据源
    @Resource
    private RedisTemplate<String,Object> redisTemplate;
//        放入数据
    @Override
    public void setValue(String key, Object value) {
        redisTemplate.opsForValue().set(key,value);
    }
//取出数据
    @Override
    public Object getValue(String key) {
        return redisTemplate.opsForValue().get(key);
    }
//删除数据
    @Override
    public Boolean removeValue(String key) {
        return redisTemplate.delete(key);
    }

    @Override
    public Boolean expire(String key, Long timeOut, TimeUnit timeUnit) {
        return redisTemplate.expire(key,timeOut,timeUnit);
    }

    @Override
    public List<Object> rangeAll(String key) {
        return redisTemplate.opsForList().range(key, 0, -1);
    }

    @Override
    public <V> Long listPush(String key, Collection<V> collection) {
        Object[] t = new Object[collection.size()];
        collection.toArray(t);
        return redisTemplate.opsForList().leftPushAll(key,t);
    }
}
package com.alatus.service.impl;

import com.alatus.service.RedisService;
import jakarta.annotation.Resource;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Collection;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Component
public class RedisServiceImpl implements RedisService {
//    数据源
    @Resource
    private RedisTemplate<String,Object> redisTemplate;
//        放入数据
    @Override
    public void setValue(String key, Object value) {
        redisTemplate.opsForValue().set(key,value);
    }
//取出数据
    @Override
    public Object getValue(String key) {
        return redisTemplate.opsForValue().get(key);
    }
//删除数据
    @Override
    public Boolean removeValue(String key) {
        return redisTemplate.delete(key);
    }

    @Override
    public Boolean expire(String key, Long timeOut, TimeUnit timeUnit) {
        return redisTemplate.expire(key,timeOut,timeUnit);
    }

    @Override
    public List<Object> rangeAll(String key) {
        return redisTemplate.opsForList().range(key, 0, -1);
    }

    @Override
    public <V> Long listPush(String key, Collection<V> collection) {
        Object[] t = new Object[collection.size()];
        collection.toArray(t);
        return redisTemplate.opsForList().leftPushAll(key,t);
    }
}
package com.alatus.service;

import java.util.Collection;
import java.util.List;
import java.util.concurrent.TimeUnit;

public interface RedisService {
    void setValue(String key,Object value);
    Object getValue(String key);
    Boolean removeValue(String key);
    Boolean expire(String key, Long timeOut, TimeUnit timeUnit);

    List<Object> rangeAll(String key);
    <V> Long listPush(String key, Collection<V> values);
}
package com.alatus.service;

import java.util.Collection;
import java.util.List;
import java.util.concurrent.TimeUnit;

public interface RedisService {
    void setValue(String key,Object value);
    Object getValue(String key);
    Boolean removeValue(String key);
    Boolean expire(String key, Long timeOut, TimeUnit timeUnit);

    List<Object> rangeAll(String key);
    <V> Long listPush(String key, Collection<V> values);
}
package com.alatus.manager;

import com.alatus.constant.Constants;
import com.alatus.service.RedisService;
import jakarta.annotation.Resource;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Collection;
import java.util.concurrent.TimeUnit;

@Component
public class RedisManager {
    @Resource
    private RedisTemplate<String,Object> redisTemplate;

    @Resource
    private RedisService redisService;

    public Object getValue(String key){
        Object object = redisService.rangeAll(key);
        redisService.expire(key, Constants.DEFAULT_KEY_EXPIRE_TIME, TimeUnit.SECONDS);
        return object;
    }

    public <T> Object setValue(String key, Collection<T> data){
        Long result = redisService.listPush(key, data);
        redisService.expire(key, Constants.DEFAULT_KEY_EXPIRE_TIME, TimeUnit.SECONDS);
        return result;
    }
}
package com.alatus.manager;

import com.alatus.constant.Constants;
import com.alatus.service.RedisService;
import jakarta.annotation.Resource;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Collection;
import java.util.concurrent.TimeUnit;

@Component
public class RedisManager {
    @Resource
    private RedisTemplate<String,Object> redisTemplate;

    @Resource
    private RedisService redisService;

    public Object getValue(String key){
        Object object = redisService.rangeAll(key);
        redisService.expire(key, Constants.DEFAULT_KEY_EXPIRE_TIME, TimeUnit.SECONDS);
        return object;
    }

    public <T> Object setValue(String key, Collection<T> data){
        Long result = redisService.listPush(key, data);
        redisService.expire(key, Constants.DEFAULT_KEY_EXPIRE_TIME, TimeUnit.SECONDS);
        return result;
    }
}

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-02-17 01:40:01       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-02-17 01:40:01       100 阅读
  3. 在Django里面运行非项目文件

    2024-02-17 01:40:01       82 阅读
  4. Python语言-面向对象

    2024-02-17 01:40:01       91 阅读

热门阅读

  1. js 解构赋值

    2024-02-17 01:40:01       59 阅读
  2. 字节跳动CEO梁汝波:要逃逸平庸的重力

    2024-02-17 01:40:01       36 阅读
  3. Rust中打印语句为什么使用宏实现?

    2024-02-17 01:40:01       54 阅读
  4. LevelDB源码阅读笔记(0、下载编译leveldb)

    2024-02-17 01:40:01       54 阅读
  5. socket编程

    2024-02-17 01:40:01       52 阅读
  6. Selenium折线图自动化测试

    2024-02-17 01:40:01       48 阅读
  7. C#继承IList 接口的设计方法

    2024-02-17 01:40:01       46 阅读