Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.magnifi.pennantrace.redis;
- import redis.clients.jedis.Jedis;
- import redis.clients.jedis.JedisPool;
- import redis.clients.jedis.JedisPoolConfig;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.util.Properties;
- public class RedisUtils {
- private static JedisPool pool;
- private static String host;
- private static Integer timeout;
- // Properties pro;
- // public static final ThreadLocal<EntityManager> entityManager = new
- // ThreadLocal<EntityManager>();
- private RedisUtils() {
- }
- public static void shutDownPool() {
- if (pool != null) {
- pool.destroy();
- pool = null;
- }
- }
- public static void returnResource(Jedis resource) {
- if (pool != null) {
- pool.returnResource(resource);
- }
- }
- public static JedisPool getRedisPoolInstance() {
- loadProperties();
- if (null == pool) {
- //String host = "ec2-54-213-145-47.us-west-2.compute.amazonaws.com";
- JedisPoolConfig poolConfig = new JedisPoolConfig();
- poolConfig.setMaxIdle(1000);
- // poolConfig.setMaxTotal(1000);
- pool = new JedisPool(poolConfig, host, 6379);
- }
- return pool;
- }
- private static void loadProperties() {
- Properties pro = new Properties();
- try {
- pro.load(RedisUtils.class.getClassLoader().getResourceAsStream(
- "game-settings.properties"));
- } catch (FileNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- if (null == host) {
- host = pro.getProperty("redis.host",
- "ec2-52-74-127-66.ap-southeast-1.compute.amazonaws.com");
- }
- if (null == timeout) {
- timeout = Integer
- .parseInt(pro.getProperty("redis.timeout", "10000"));
- }
- }
- public static void add(String key, String value) {
- Jedis jd = RedisUtils.getRedisPoolInstance().getResource();
- if (!jd.exists(key)) {
- jd.set(key, value);
- jd.expire(key, 600);
- }
- }
- public static boolean isValid(String key, String valueToCheck) {
- Jedis jd = RedisUtils.getRedisPoolInstance().getResource();
- if (jd.exists(key) && jd.get(key).equals(valueToCheck)) {
- return true;
- }
- return false;
- }
- public static void main(String[] args) {
- JedisPool pool = RedisUtils.getRedisPoolInstance();
- Jedis jd = pool.getResource();
- jd.set("viet", "value");
- System.out.println("RedisUtils.main()" + jd.get("28"));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement