Created
October 8, 2018 19:02
-
-
Save djangofan/29da1810cc95e1e112eec009a101526e to your computer and use it in GitHub Desktop.
Example of JedisServiceImpl class in Java.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.tb.services; | |
import java.util.Collection; | |
import java.util.List; | |
import java.util.Map; | |
import java.util.Set; | |
import java.util.concurrent.TimeUnit; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.data.redis.core.RedisTemplate; | |
import org.springframework.stereotype.Service; | |
@Service | |
public class JedisServiceImpl implements JedisService { | |
@Autowired | |
private RedisTemplate<String, Object> template; | |
// Save String key value | |
@Override | |
public void save(String key, String value) { | |
template.opsForValue().set(key, value); | |
} | |
// Save String key value with expire time in seconds | |
@Override | |
public void save(String key, String value, long expiryInSeconds) { | |
template.opsForValue().set(key, value); | |
template.expire(key, expiryInSeconds, TimeUnit.SECONDS); | |
} | |
// Append a string value to existing key | |
@Override | |
public int append(String key, String valueToAppend) { | |
int added = 0; | |
if (template.hasKey(key)) | |
added = template.opsForValue().append(key, valueToAppend); | |
return added; | |
} | |
// Check if a key exists | |
@Override | |
public boolean exists(String key) { | |
return template.hasKey(key); | |
} | |
// Get String key value | |
@Override | |
public Object get(String key) { | |
return template.opsForValue().get(key); | |
} | |
// Delete a key | |
@Override | |
public void delete(String key) { | |
template.delete(key); | |
} | |
// Get expire duration of a key in second | |
@Override | |
public long getExpireDuration(String key) { | |
return template.getExpire(key, TimeUnit.SECONDS); | |
} | |
// Get keys of a pettern | |
@Override | |
public Set<String> getKeysForAPattern(String pattern) { | |
return template.keys(pattern + "*"); | |
} | |
// Get multiple values by multiple keys | |
@Override | |
public List<Object> multiGet(Collection<String> keys) { | |
return template.opsForValue().multiGet(keys); | |
} | |
// Set multiple values by multiple keys | |
@Override | |
public void multiSet(Map<String, Object> keyValuePair) { | |
template.opsForValue().multiSet(keyValuePair); | |
} | |
// Set expire duration of a key | |
@Override | |
public boolean removeExpireDuration(String key) { | |
return template.persist(key); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment