經常使用代碼1

1.獲取特定的日期格式redis

SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");  
String datetime = sdf.format(new Date()); 
String dateStr = datetime.substring(0, 7) ;
String timeStr = datetime.substring(8) ;

 

2.先從redis.properties文件讀取redis的配置,而後經過手動建立Redis鏈接讀取Redis中的配置ui

Properties props = new Properties();
ClassLoader loader = RedisConfigurer.class.getClassLoader();
InputStream in = loader.getResourceAsStream("redis.properties");
props.load(in);
// 建立redis鏈接
String host = props.getProperty("redis.host").trim();
int port = Integer.parseInt(props.getProperty("redis.port").trim());
String password = props.getProperty("redis.password");
int database = Integer.parseInt(props.getProperty("redis.database"));
Jedis jedis = null ;
try{
      jedis =new Jedis(host, port);
// 鑑權信息
jedis.auth(password);
jedis.select(database);
    
      Set<String> set = jedis.keys( prefix + ".*") ;
            
      Iterator<String> iterator = set.iterator() ;
      while(iterator.hasNext()){
                String key = iterator.next() ;
                String value = jedis.get(key) ;
                LOGGER.debug("redis key=" + key + ",value=" + value);
                props.put(key.substring(prefix.length()+1), value) ;
     }
}catch(Exception e){
     e.printStackTrace();
}finally{
     if(jedis != null){
          jedis.quit(); 
          jedis.close();
     }
}
相關文章
相關標籤/搜索