自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Servlet3.0筆記之Redis操作示范Retwis Java版

開發(fā) 后端 Redis
Retwis-JAVA,基于Servlet 3.0 + UrlRewrite + Freemarker + Jedis。示范運行在Tomcat 7中,redis為最新的2.22版本,jedis為redis的java客戶端操作框架。

Retwis-JAVA,基于Servlet 3.0 + UrlRewrite + Freemarker + Jedis。示范運行在Tomcat 7中,redis為***的2.22版本,jedis為redis的java客戶端操作框架。在Servlet 3.0規(guī)范中,對Url映射處理依然沒有進步,因此只能使用UrlRewrite框架讓部分url看起來友好一些。另外,項目沒有使用IOC容器框架,沒有使用MVC框架,代碼量稍微多些,代碼相對耦合一些。若使用Struts2 + Spring 代碼量會少一些。

對涉及到的redis存儲結(jié)構(gòu),大致如下:

 

 

涉及到的兩個對象很簡單:

 

 

序列化后以二進制數(shù)據(jù)保存到redis中:

 

  1. private   byte [] object2Bytes(V value) {  
  2.          if  (value  ==   null )  
  3.              return   null ;  
  4.  
  5.         ByteArrayOutputStream arrayOutputStream  =   new  ByteArrayOutputStream();  
  6.         ObjectOutputStream outputStream;  
  7.          try  {  
  8.             outputStream  =   new  ObjectOutputStream(arrayOutputStream);  
  9.  
  10.             outputStream.writeObject(value);  
  11.         }  catch  (IOException e) {  
  12.             e.printStackTrace();  
  13.         }  finally  {  
  14.              try  {  
  15.                 arrayOutputStream.close();  
  16.             }  catch  (IOException e) {  
  17.                 e.printStackTrace();  
  18.             }  
  19.         }  
  20.  
  21.          return  arrayOutputStream.toByteArray();  
  22.     }  
  23.  
  24.  
  25.      public   void  save(String key, V value) {  
  26.         jedis.set(getKey(key), object2Bytes(value));  
  27.     }  

 

獲取用戶的timeline時,redis的LRANGE命令提供對list類型數(shù)據(jù)提供分頁操作:

 

  1. private  List < Status >  timeline(String targetId,  int  page) {  
  2.          if  (page  <   1 )  
  3.             page  =   1 ;  
  4.  
  5.          int  startIndex  =  (page  -   1 )  *   10 ;  
  6.          int  endIndex  =  page  *   10 ;  
  7.  
  8.         List < String >  idList  =   super .jedis  
  9.                 .lrange(targetId, startIndex, endIndex);  
  10.  
  11.          if  (idList.isEmpty())  
  12.              return   new  ArrayList < Status > ( 0 );  
  13.  
  14.         List < Status >  statusList  =   new  ArrayList < Status > (idList.size());  
  15.          for  (String id : idList) {  
  16.             Status status  =  load(Long.valueOf(id));  
  17.  
  18.              if  (status  ==   null )  
  19.                  continue ;  
  20.  
  21.             status.setUser(userService.load(status.getUid()));  
  22.  
  23.             statusList.add(status);  
  24.         }  
  25.  
  26.          return  statusList;  
  27.     }  

 

很顯然,LRANGE取出了Status對象的ID,然后我們需要再次根據(jù)ID獲取對應(yīng)的Status對象二進制數(shù)據(jù),然后反序列化:

 

  1. public  Status load( long  id) {  
  2.         return   super .get(getFormatId(id));  
  3.    }  
  4.  
  5.     private  String getFormatId( long  id) {  
  6.         return  String.format(STATUS_ID_FORMAT, id);  
  7.    }  
  8.  
  9.     private   static   final  String STATUS_ID_FORMAT  =   " status:id:%d " ;  
  10.  
  11.     public  V get(String key) {  
  12.         return  byte2Object(jedis.get(getKey(key)));  
  13.    }  
  14.  
  15.    @SuppressWarnings" unchecked " )  
  16.     private  V byte2Object( byte [] bytes) {  
  17.         if  (bytes  ==   null   ||  bytes.length  ==   0 )  
  18.             return   null ;  
  19.  
  20.         try  {  
  21.            ObjectInputStream inputStream;  
  22.            inputStream  =   new  ObjectInputStream( new  ByteArrayInputStream(bytes));  
  23.            Object obj  =  inputStream.readObject();  
  24.  
  25.             return  (V) obj;  
  26.        }  catch  (IOException e) {  
  27.            e.printStackTrace();  
  28.        }  catch  (ClassNotFoundException e) {  
  29.            e.printStackTrace();  
  30.        }  
  31.  
  32.         return   null ;  
  33.    }  

 

以上使用JDK內(nèi)置的序列化支持;更多序列化,可參考hessian、google protobuf等序列化框架,后者提供業(yè)界更為成熟的跨平臺、更為高效的序列化方案。更多代碼請參見附件。

一些總結(jié)和思考:

不僅僅是緩存,替代SQL數(shù)據(jù)庫已完全成為可能,更高效,更經(jīng)濟;雖然只是打開了一扇小的窗戶,但說不準(zhǔn)以后人們會把大門打開。

實際環(huán)境中,可能***方式為SQL + NOSQL配合使用,互相彌補不足;還好,redis指令不算多,可速查,簡單易記。

JAVA和RUBY代碼相比,有些重

另:

在線版,請參考 http://retwisrb.danlucraft.com/。那個誰誰,要運行范例,保證redis運行才行。

【編輯推薦】

  1. Java Web應(yīng)用開發(fā)中的一些概念
  2. Tomcat 7 應(yīng)用實測:聲明式Servlet 3.0
  3. 探秘Servlet 3.0中的Web安全改進
  4. 簡化Web應(yīng)用開發(fā) Servlet 3.0特性詳解
  5. Servlet 3.0的異步處理
責(zé)任編輯:金賀 來源: ITEYE博客
相關(guān)推薦

2011-05-13 17:36:05

HTML

2010-06-30 08:47:04

Tomcat 7ApacheServlet 3.0

2009-07-08 09:35:53

Java ServleServlet 3.0

2021-04-15 12:30:18

ServletSpringMVC 版本

2010-01-14 09:15:07

Java EE 6Servlet 3.0異步處理

2012-04-09 15:06:38

筆記本評測

2022-10-11 08:37:43

Servlet配置版本

2011-09-04 10:38:48

筆記本簡評

2009-06-15 09:12:45

Servlet 3.0

2010-05-11 16:22:40

2011-09-30 10:03:22

神舟筆記本

2019-01-03 10:40:15

加速Windows 10筆記本電腦

2021-07-09 07:00:27

SpringbootServlet容器

2011-10-08 16:28:04

微星筆記本

2011-10-07 12:15:20

ThinkPad筆記本

2011-10-27 14:26:49

ThinkPad筆記本

2011-05-07 20:24:01

ThinkPad試用

2011-09-10 10:23:36

筆記本行情

2011-07-25 22:58:56

戴爾筆記本
點贊
收藏

51CTO技術(shù)棧公眾號