2013-06-29 11:20:53 +02:00
|
|
|
package com.commafeed.backend.cache;
|
|
|
|
|
|
2014-12-12 10:19:32 +01:00
|
|
|
import java.util.ArrayList;
|
2013-06-29 11:20:53 +02:00
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Set;
|
|
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
|
|
|
|
import com.commafeed.backend.model.Feed;
|
2013-07-25 10:21:11 +02:00
|
|
|
import com.commafeed.backend.model.FeedSubscription;
|
2013-07-02 18:07:08 +02:00
|
|
|
import com.commafeed.backend.model.Models;
|
|
|
|
|
import com.commafeed.backend.model.User;
|
2013-07-25 10:38:23 +02:00
|
|
|
import com.commafeed.frontend.model.Category;
|
2013-08-08 17:00:35 +02:00
|
|
|
import com.commafeed.frontend.model.UnreadCount;
|
2013-07-02 18:07:08 +02:00
|
|
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
2013-06-29 11:20:53 +02:00
|
|
|
|
2022-01-02 20:55:46 +01:00
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
import redis.clients.jedis.Jedis;
|
|
|
|
|
import redis.clients.jedis.JedisPool;
|
|
|
|
|
import redis.clients.jedis.Pipeline;
|
|
|
|
|
|
2013-08-11 11:45:32 +02:00
|
|
|
@Slf4j
|
2014-09-22 09:51:55 +02:00
|
|
|
@RequiredArgsConstructor
|
2013-06-29 11:20:53 +02:00
|
|
|
public class RedisCacheService extends CacheService {
|
|
|
|
|
|
2022-01-02 20:55:46 +01:00
|
|
|
private static final ObjectMapper MAPPER = new ObjectMapper();
|
2013-07-02 18:07:08 +02:00
|
|
|
|
2014-09-22 09:51:55 +02:00
|
|
|
private final JedisPool pool;
|
2013-06-29 11:20:53 +02:00
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public List<String> getLastEntries(Feed feed) {
|
2014-12-12 10:19:32 +01:00
|
|
|
List<String> list = new ArrayList<>();
|
2014-09-23 15:21:34 +02:00
|
|
|
try (Jedis jedis = pool.getResource()) {
|
2013-07-02 18:07:08 +02:00
|
|
|
String key = buildRedisEntryKey(feed);
|
2013-06-29 11:20:53 +02:00
|
|
|
Set<String> members = jedis.smembers(key);
|
|
|
|
|
for (String member : members) {
|
|
|
|
|
list.add(member);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return list;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void setLastEntries(Feed feed, List<String> entries) {
|
2014-09-23 15:21:34 +02:00
|
|
|
try (Jedis jedis = pool.getResource()) {
|
2013-07-02 18:07:08 +02:00
|
|
|
String key = buildRedisEntryKey(feed);
|
2013-06-29 11:20:53 +02:00
|
|
|
|
|
|
|
|
Pipeline pipe = jedis.pipelined();
|
|
|
|
|
pipe.del(key);
|
|
|
|
|
for (String entry : entries) {
|
|
|
|
|
pipe.sadd(key, entry);
|
|
|
|
|
}
|
2013-07-02 10:48:54 +02:00
|
|
|
pipe.expire(key, (int) TimeUnit.DAYS.toSeconds(7));
|
2013-06-29 11:20:53 +02:00
|
|
|
pipe.sync();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-07-02 18:07:08 +02:00
|
|
|
@Override
|
2013-07-25 10:38:23 +02:00
|
|
|
public Category getUserRootCategory(User user) {
|
|
|
|
|
Category cat = null;
|
2014-09-23 15:21:34 +02:00
|
|
|
try (Jedis jedis = pool.getResource()) {
|
2013-07-25 10:38:23 +02:00
|
|
|
String key = buildRedisUserRootCategoryKey(user);
|
2013-07-02 18:07:08 +02:00
|
|
|
String json = jedis.get(key);
|
|
|
|
|
if (json != null) {
|
2014-09-22 09:51:55 +02:00
|
|
|
cat = MAPPER.readValue(json, Category.class);
|
2013-07-02 18:07:08 +02:00
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
log.error(e.getMessage(), e);
|
|
|
|
|
}
|
2013-07-25 10:38:23 +02:00
|
|
|
return cat;
|
2013-07-02 18:07:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2013-07-25 10:38:23 +02:00
|
|
|
public void setUserRootCategory(User user, Category category) {
|
2014-09-23 15:21:34 +02:00
|
|
|
try (Jedis jedis = pool.getResource()) {
|
2013-07-25 10:38:23 +02:00
|
|
|
String key = buildRedisUserRootCategoryKey(user);
|
2013-07-02 18:07:08 +02:00
|
|
|
|
|
|
|
|
Pipeline pipe = jedis.pipelined();
|
|
|
|
|
pipe.del(key);
|
2014-09-22 09:51:55 +02:00
|
|
|
pipe.set(key, MAPPER.writeValueAsString(category));
|
2013-07-03 12:42:01 +02:00
|
|
|
pipe.expire(key, (int) TimeUnit.MINUTES.toSeconds(30));
|
|
|
|
|
pipe.sync();
|
|
|
|
|
} catch (JsonProcessingException e) {
|
|
|
|
|
log.error(e.getMessage(), e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2013-08-08 17:00:35 +02:00
|
|
|
public UnreadCount getUnreadCount(FeedSubscription sub) {
|
|
|
|
|
UnreadCount count = null;
|
2014-09-23 15:21:34 +02:00
|
|
|
try (Jedis jedis = pool.getResource()) {
|
2013-07-25 10:21:11 +02:00
|
|
|
String key = buildRedisUnreadCountKey(sub);
|
2013-08-08 17:00:35 +02:00
|
|
|
String json = jedis.get(key);
|
|
|
|
|
if (json != null) {
|
2014-09-22 09:51:55 +02:00
|
|
|
count = MAPPER.readValue(json, UnreadCount.class);
|
2013-07-25 10:21:11 +02:00
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
log.error(e.getMessage(), e);
|
|
|
|
|
}
|
|
|
|
|
return count;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2013-08-08 17:00:35 +02:00
|
|
|
public void setUnreadCount(FeedSubscription sub, UnreadCount count) {
|
2014-09-23 15:21:34 +02:00
|
|
|
try (Jedis jedis = pool.getResource()) {
|
2013-07-25 10:21:11 +02:00
|
|
|
String key = buildRedisUnreadCountKey(sub);
|
|
|
|
|
|
2013-07-02 18:07:08 +02:00
|
|
|
Pipeline pipe = jedis.pipelined();
|
2013-07-25 10:21:11 +02:00
|
|
|
pipe.del(key);
|
2014-09-22 09:51:55 +02:00
|
|
|
pipe.set(key, MAPPER.writeValueAsString(count));
|
2013-07-25 10:21:11 +02:00
|
|
|
pipe.expire(key, (int) TimeUnit.MINUTES.toSeconds(30));
|
|
|
|
|
pipe.sync();
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
log.error(e.getMessage(), e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2013-07-25 10:38:23 +02:00
|
|
|
public void invalidateUserRootCategory(User... users) {
|
2014-09-23 15:21:34 +02:00
|
|
|
try (Jedis jedis = pool.getResource()) {
|
2013-07-25 10:38:23 +02:00
|
|
|
Pipeline pipe = jedis.pipelined();
|
|
|
|
|
if (users != null) {
|
|
|
|
|
for (User user : users) {
|
|
|
|
|
String key = buildRedisUserRootCategoryKey(user);
|
|
|
|
|
pipe.del(key);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
pipe.sync();
|
2013-07-25 10:21:11 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void invalidateUnreadCount(FeedSubscription... subs) {
|
2014-09-23 15:21:34 +02:00
|
|
|
try (Jedis jedis = pool.getResource()) {
|
2013-07-25 10:21:11 +02:00
|
|
|
Pipeline pipe = jedis.pipelined();
|
|
|
|
|
if (subs != null) {
|
|
|
|
|
for (FeedSubscription sub : subs) {
|
|
|
|
|
String key = buildRedisUnreadCountKey(sub);
|
2013-07-03 12:42:01 +02:00
|
|
|
pipe.del(key);
|
2013-07-02 18:07:08 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
pipe.sync();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-07-25 10:21:11 +02:00
|
|
|
private String buildRedisEntryKey(Feed feed) {
|
|
|
|
|
return "f:" + Models.getId(feed);
|
2013-07-02 18:07:08 +02:00
|
|
|
}
|
|
|
|
|
|
2013-07-25 10:38:23 +02:00
|
|
|
private String buildRedisUserRootCategoryKey(User user) {
|
2013-07-25 10:21:11 +02:00
|
|
|
return "c:" + Models.getId(user);
|
2013-07-03 12:42:01 +02:00
|
|
|
}
|
|
|
|
|
|
2013-07-25 10:21:11 +02:00
|
|
|
private String buildRedisUnreadCountKey(FeedSubscription sub) {
|
|
|
|
|
return "u:" + Models.getId(sub);
|
2013-06-29 11:20:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|