Advertisement
yerzhik

Untitled

Jul 21st, 2015
300
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.48 KB | None | 0 0
  1. package com.magnifi.pennantrace.redis;
  2.  
  3. import redis.clients.jedis.Jedis;
  4. import redis.clients.jedis.JedisPool;
  5. import redis.clients.jedis.JedisPoolConfig;
  6.  
  7. import java.io.FileNotFoundException;
  8. import java.io.IOException;
  9. import java.util.Properties;
  10.  
  11. public class RedisUtils {
  12.     private static JedisPool pool;
  13.     private static String host;
  14.     private static Integer timeout;
  15.  
  16.     // Properties pro;
  17.     // public static final ThreadLocal<EntityManager> entityManager = new
  18.     // ThreadLocal<EntityManager>();
  19.     private RedisUtils() {
  20.  
  21.     }
  22.  
  23.     public static void shutDownPool() {
  24.         if (pool != null) {
  25.             pool.destroy();
  26.             pool = null;
  27.         }
  28.     }
  29.     public static void returnResource(Jedis resource) {
  30.         if (pool != null) {
  31.             pool.returnResource(resource);
  32.         }
  33.     }
  34.  
  35.     public static JedisPool getRedisPoolInstance() {
  36.         loadProperties();
  37.         if (null == pool) {
  38.             //String host = "ec2-54-213-145-47.us-west-2.compute.amazonaws.com";
  39.             JedisPoolConfig poolConfig = new JedisPoolConfig();
  40.             poolConfig.setMaxIdle(1000);
  41. //            poolConfig.setMaxTotal(1000);
  42.             pool = new JedisPool(poolConfig, host, 6379);
  43.         }
  44.         return pool;
  45.     }
  46.  
  47.     private static void loadProperties() {
  48.         Properties pro = new Properties();
  49.  
  50.         try {
  51.             pro.load(RedisUtils.class.getClassLoader().getResourceAsStream(
  52.                     "game-settings.properties"));
  53.         } catch (FileNotFoundException e) {
  54.             // TODO Auto-generated catch block
  55.             e.printStackTrace();
  56.         } catch (IOException e) {
  57.             // TODO Auto-generated catch block
  58.             e.printStackTrace();
  59.         }
  60.  
  61.         if (null == host) {
  62.             host = pro.getProperty("redis.host",
  63.                     "ec2-52-74-127-66.ap-southeast-1.compute.amazonaws.com");
  64.         }
  65.         if (null == timeout) {
  66.             timeout = Integer
  67.                     .parseInt(pro.getProperty("redis.timeout", "10000"));
  68.         }
  69.     }
  70.  
  71.     public static void add(String key, String value) {
  72.         Jedis jd = RedisUtils.getRedisPoolInstance().getResource();
  73.         if (!jd.exists(key)) {
  74.             jd.set(key, value);
  75.             jd.expire(key, 600);
  76.         }
  77.     }
  78.  
  79.     public static boolean isValid(String key, String valueToCheck) {
  80.         Jedis jd = RedisUtils.getRedisPoolInstance().getResource();
  81.         if (jd.exists(key) && jd.get(key).equals(valueToCheck)) {
  82.             return true;
  83.         }
  84.         return false;
  85.     }
  86.  
  87.     public static void main(String[] args) {
  88.         JedisPool pool = RedisUtils.getRedisPoolInstance();
  89.         Jedis jd = pool.getResource();
  90.         jd.set("viet", "value");
  91.         System.out.println("RedisUtils.main()" + jd.get("28"));
  92.  
  93.     }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement