我有一个SQL查询
SELECT COUNT(*)
FROM (SELECT *
FROM recipes
WHERE lock != '') AS count
我希望每当结果发生变化时都会收到通知。当我只在值为0或> 0时收到通知时,这将是理想的。有没有人有解决方案?
答案 0 :(得分:1)
在@Configuration
@EnableCaching
@EnableRedisHttpSession
public class RedisConfig {
@Value("${spring.redis.host}")
private String host;
@Bean
public static ConfigureRedisAction configureRedisAction() {
return ConfigureRedisAction.NO_OP;
}
@Bean
public JedisPoolConfig jedisPoolConfig() {
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
jedisPoolConfig.setMaxTotal(128);
jedisPoolConfig.setMaxIdle(128);
jedisPoolConfig.setMinIdle(16);
jedisPoolConfig.setTestOnBorrow(true);
jedisPoolConfig.setTestOnReturn(true);
jedisPoolConfig.setTestWhileIdle(true);
jedisPoolConfig.setMinEvictableIdleTimeMillis(
Duration.ofSeconds(60).toMillis());
jedisPoolConfig.setTimeBetweenEvictionRunsMillis(
Duration.ofSeconds(30).toMillis());
jedisPoolConfig.setNumTestsPerEvictionRun(3);
jedisPoolConfig.setBlockWhenExhausted(true);
return jedisPoolConfig;
}
@Bean
public JedisConnectionFactory jedisConnectionFactory() {
JedisConnectionFactory jedisConnectionFactory = new
JedisConnectionFactory(jedisPoolConfig());
jedisConnectionFactory.setHostName(host);
jedisConnectionFactory.setPort(6379);
jedisConnectionFactory.afterPropertiesSet();
return jedisConnectionFactory;
}
@Bean
public RedisTemplate<String, Object> redisTemplate() {
RedisTemplate<String, Object> template = new RedisTemplate<>();
if (null == jedisConnectionFactory()) {
logger.error("Redis template service is not available");
return null;
};
template.setConnectionFactory(jedisConnectionFactory());
template.afterPropertiesSet();
return template;
}
@Bean
public RedisCacheManager redisCacheManager() {
Map<String, Long> expires = new HashMap<>();
expires.put("user:profile", 300L);
RedisCacheManager redisCacheManager = new
RedisCacheManager(redisTemplate());
redisCacheManager.setUsePrefix(true);
redisCacheManager.setDefaultExpiration(300);
redisCacheManager.setExpires(expires);
return redisCacheManager;
}
@Bean
public KeyGenerator keyGenerator() {
return new KeyGenerator() {
@Override
public Object generate(Object o, Method method, Object... objects) {
StringBuilder sb = new StringBuilder();
sb.append(o.getClass().getName());
sb.append(method.getName());
for (Object obj : objects) {
sb.append(obj.toString());
}
return sb.toString();
}
};
}
}
上创建触发器:
recipes
在通道create or replace function recipes_trigger()
returns trigger language plpgsql as $$
declare
payload text;
begin
payload:= exists(select 1 from recipes where lock <> '')::int;
perform pg_notify('recipes', payload);
return null;
end $$;
create trigger recipes_trigger
after insert or update or delete on recipes
for each statement execute procedure recipes_trigger();
上收听的客户端将在表格上的每次插入/更新/删除后收到有效负载recipes
或0
的通知。