redis springBoot中的使用 Sucha

Administrator
发布于 2020-07-22 / 1211 阅读 / 0 评论 / 0 点赞

redis springBoot中的使用 Sucha

redis springBoot中的使用 Sucha

1、使用RedisOperations对List(其实Object也可以)进行存取

  • 示例list操作
List<WeekMenuStaff> weekMenuList = weekMenuService.getWxWeekMenuList();
 //把list放入redis
redisStringDao.saveOrUpdate(
    RedisConst.KEY_PREFIX_TYPE_STAFF_WEEK_MENU_LIST, currentOrgId(),
    weekMenuList, 1000);
//获取
redisStringDao.get(RedisConst.KEY_PREFIX_TYPE_STAFF_WEEK_MENU_LIST, currentOrgId())
  • RedisStringDaoImpl实现类,其中有saveOrUpdate方法
/**
 * 通用String型参数操作实现类
 *
 * @author Kamisama
 * @date 2017/10/30
 */
@Repository("redisStringDao")
public class RedisStringDaoImpl implements RedisStringDao {

    private static final String KEY_PREFIX = "husk:stringValue:";

    /**
     * Redis操作对象
     */
    private RedisOperations<String, Object> redisOperations;

    public RedisStringDaoImpl() {
    }

    @Autowired(required = false)
     public RedisStringDaoImpl(RedisOperations redisTemplate) {
        this.redisOperations = redisTemplate;
    }


    @Override
    public void saveOrUpdate(String type, String name, Object value, long timeoutMinutes) {
        redisOperations.boundValueOps(getKey(type, name)).set(value, timeoutMinutes, TimeUnit.MINUTES);
    }

    @Override
    public Object get(String type, String name) {
        return redisOperations.boundValueOps(getKey(type, name)).get();
    }

    /**
     * 取得参数在Redis库里对应的键
     * @param type 参数类型
     * @param name 参数名
     * @return 键
     */
    private String getKey(String type, String name) {
        return KEY_PREFIX + type + ":" + name;
    }

}

评论