Giriş
Şu satırı dahil ederiz
import org.springframework.data.redis.core.ValueOperations;
get metodu
Elimizde şöyle bir kod olsun.
private RedisTemplate<String, Object> redisTemplate = ...;
Şöyle yaparız.
ValueOperations<String, String> valueOperations = redisTemplate.opsForValue();
String key = ...;
String value = valueOperations.get(key);
Örnek
Şöyle yaparız
RedisTemplate<String, Object> redisTemplate = ...;private Optional<KeyValuePair> checkInCache(String key) {try {Object entryInCache = redisTemplate.opsForValue().get(key);return Optional.of(objectMapper.readValue(entryInCache.toString(),KeyValuePair.class));} catch (Exception e) {...}return Optional.empty();}@Overridepublic KeyValuePair findByKey(String key) {return checkInCache(key).orElseGet(()->this.getFromDb(key));}
increment metodu
Örnek
Şöyle yaparız
String name = ... long count = redisTemplate.opsForValue().increment(name, 1);
set metodu
Örnek
Şöyle yaparız
@Componentpublic class RedisUtil<T> {private RedisTemplate<String, T> redisTemplate;private ValueOperations<String, T> valueOperations;@Autowiredpublic RedisUtil(RedisTemplate<String, T> redisTemplate) {this.redisTemplate = redisTemplate;this.valueOperations = redisTemplate.opsForValue();}//region Stringpublic void putValue(String key, T value) {valueOperations.set(key, value);}public T getValue(String key) {return valueOperations.get(key);}...}
Hiç yorum yok:
Yorum Gönder