新增缓存值函数

This commit is contained in:
谷成伟 2024-11-28 16:10:45 +08:00
parent 4ade32744e
commit ef5aaa5a2c
2 changed files with 27 additions and 21 deletions

View File

@ -1,11 +1,6 @@
package com.das.modules.calc.functions;
import com.das.common.utils.AdminRedisTemplate;
import com.das.modules.cache.domain.DeviceInfoCache;
import com.das.modules.cache.service.CacheService;
import com.das.modules.data.domain.SnapshotValueQueryParam;
import com.das.modules.data.service.DataService;
import com.googlecode.aviator.exception.StandardError;
import com.googlecode.aviator.runtime.function.AbstractFunction;
import com.googlecode.aviator.runtime.type.AviatorNil;
import com.googlecode.aviator.runtime.type.AviatorObject;
@ -13,8 +8,6 @@ import com.googlecode.aviator.runtime.type.AviatorRuntimeJavaType;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
@ -24,9 +17,11 @@ import java.util.concurrent.ConcurrentHashMap;
* 函数格式: cacheValue(key) 读取缓存值
*/
@Slf4j
public class FunctionGetCacheValue extends AbstractFunction {
private ConcurrentHashMap<String,Double> cacheValues = new ConcurrentHashMap<>();
public FunctionGetCacheValue( ) {
public class FunctionCacheValue extends AbstractFunction {
public static final String CACHE_PREFIX = "calc::cache::";
private AdminRedisTemplate redis = null;
public FunctionCacheValue(AdminRedisTemplate redis) {
this.redis = redis;
}
@Override
@ -42,9 +37,22 @@ public class FunctionGetCacheValue extends AbstractFunction {
Double value = (Double) valData.getValue(env);
String scriptName = (String)env.get("G_SCRIPTNAME");
String cacheKey = String.format("%s_%s", scriptName, key);
cacheValues.put(cacheKey, value);
String cacheKey = String.format("%s%s_%s", CACHE_PREFIX, scriptName, key);
redis.set(cacheKey, value);
return AviatorRuntimeJavaType.valueOf(0);
}
@SneakyThrows
@Override
public AviatorObject call(Map<String, Object> env, AviatorObject keyData, AviatorObject valData, AviatorObject ttlData) {
//设备Code
String key = (String)keyData.getValue(env);
Double value = (Double) valData.getValue(env);
Long ttl = (Long) ttlData.getValue(env);
String scriptName = (String)env.get("G_SCRIPTNAME");
String cacheKey = String.format("%s%s_%s", CACHE_PREFIX, scriptName, key);
redis.setEx(cacheKey, value, ttl);
return AviatorRuntimeJavaType.valueOf(0);
}
@ -55,7 +63,7 @@ public class FunctionGetCacheValue extends AbstractFunction {
String scriptName = (String)env.get("G_SCRIPTNAME");
String cacheKey = String.format("%s_%s", scriptName, key);
Double value = cacheValues.get(cacheKey);
Double value = redis.get(cacheKey);
if (value == null) {
return AviatorNil.NIL;
}

View File

@ -2,10 +2,11 @@ package com.das.modules.calc.service;
import cn.hutool.core.codec.Base64Encoder;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.das.common.utils.AdminRedisTemplate;
import com.das.modules.cache.service.CacheService;
import com.das.modules.calc.domain.entity.CalcModule;
import com.das.modules.calc.domain.vo.CalcModuleVo;
import com.das.modules.calc.functions.FunctionGetCacheValue;
import com.das.modules.calc.functions.FunctionCacheValue;
import com.das.modules.calc.functions.FunctionRealData;
import com.das.modules.calc.functions.FunctionSaveCalcData;
import com.das.modules.calc.mapper.CalcModuleMapper;
@ -21,15 +22,9 @@ import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
/**
* 计算引擎服务
@ -53,6 +48,9 @@ public class CalcService {
@Autowired
DataService dataService;
@Autowired
AdminRedisTemplate adminRedisTemplate;
/**
* 计算引擎实例
*/
@ -112,7 +110,7 @@ public class CalcService {
FunctionSaveCalcData save = new FunctionSaveCalcData(dataService, cacheService);
aviator.addFunction(save);
FunctionGetCacheValue cache = new FunctionGetCacheValue();
FunctionCacheValue cache = new FunctionCacheValue(adminRedisTemplate);
aviator.addFunction(cache);
}