Created
August 26, 2015 11:31
-
-
Save ameyawebonise/b1fbcb2c2efad47e8f79 to your computer and use it in GitHub Desktop.
This file contains hidden or 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.magnus.newpack.dao; | |
| import java.util.Collection; | |
| import java.util.HashSet; | |
| import java.util.Set; | |
| import java.util.concurrent.TimeUnit; | |
| import com.magnus.common.utils.MagnusPropertySettingsUtils; | |
| import redis.clients.jedis.*; | |
| import com.magnus.newpack.factory.JedisFactory; | |
| /** | |
| * Created by webonise on 13/7/15. | |
| */ | |
| public class RedisDao { | |
| private final JedisPoolConfig poolConfig ; | |
| private final static Integer timeout = 3; | |
| private final JedisPool pool; | |
| private final static String STARTING_CURSOR = "0"; | |
| public RedisDao(String redisHost ,int redisPort ,int databaseIndex){ | |
| poolConfig = new JedisPoolConfig(); | |
| pool = new JedisFactory.Builder(redisHost,redisPort,databaseIndex).build().getJedisPool(); | |
| } | |
| public Long add(String key,String redisCacheable) { | |
| Jedis jedis = pool.getResource(); | |
| Long result = jedis.sadd(key, redisCacheable); | |
| jedis.close(); | |
| return result; | |
| } | |
| public Set<String> get(String key) { | |
| Jedis jedis = pool.getResource(); | |
| Set<String>result = jedis.smembers(key); | |
| jedis.close(); | |
| return result; | |
| } | |
| public boolean isMember(String setKey, String UUID) { | |
| Jedis jedis = pool.getResource(); | |
| boolean result = jedis.sismember(setKey, UUID); | |
| jedis.close(); | |
| return result; | |
| } | |
| public Set<String> getKeySet(String clientId){ | |
| Set<String> keySet = new HashSet<String>(); | |
| ScanParams params = new ScanParams().match(clientId); | |
| scanKeyset(STARTING_CURSOR, params,keySet); | |
| return keySet; | |
| } | |
| private Set<String> scanKeyset(String startingCursor,ScanParams params ,Set<String> keySet){ | |
| Jedis jedis = pool.getResource(); | |
| ScanResult<String> result = jedis.scan(startingCursor, params); | |
| keySet.addAll(result.getResult()); | |
| if(!result.getStringCursor().equals(STARTING_CURSOR)){ | |
| scanKeyset(result.getStringCursor(),params,keySet); | |
| } | |
| jedis.close(); | |
| return keySet; | |
| } | |
| public long addTTLToKey(String key,int ttlInSeconds){ | |
| Jedis jedis = pool.getResource(); | |
| long result = jedis.expire(key,ttlInSeconds); | |
| return result; | |
| } | |
| public long getTTLForKey(String key){ | |
| Jedis jedis = pool.getResource(); | |
| long result = jedis.ttl(key); | |
| jedis.close(); | |
| return result; | |
| } | |
| public long deleteKeySet(Collection<String> keySet){ | |
| Jedis jedis = pool.getResource(); | |
| long result = jedis.del(keySet.toArray(new String[keySet.size()])); | |
| jedis.close(); | |
| return result; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment