Arch-03-14-キャッシュポリシー

12231 ワード

Arch-03-14-キャッシュポリシー
 
robin(肉餅という人がいて、才能がある)の一文は強くて、ここで明かりを作っています.
http://robbin.iteye.com/blog/770553
 
 
と書く
OSディスクキャッシュ
データベース・キャッシュ
--Query Cache
--Data Buffer
アプリケーションキャッシュ
--オブジェクトキャッシュ
--クエリーキャッシュ
ページの内部キャッシュ
--動的ページの静的化
--サーブレットキャッシュ
Webサーバキャッシュ
--squid/nginx
--CDN
クライアントブラウザキャッシュ
--AJAXキャッシュ
--HTTPプロトコルに基づくリソースキャッシュ
対象Cacheであれば、自然とORMが透明化します.Cacheをクエリーする場合は、DAOレイヤ処理が自然です.ActionCacheであれば、当然Web層Action処理である.Page Cacheであれば、当然サーブレットFilter処理です.
ibatis+memcachedの戦略で明らかに欠陥があり、粒度が大きすぎて、より小さな粒度で効率的な案を考えましょう.
 
hibernateのキャッシュは悪くなくて、原理はとても良くて、前のプロジェクトはずっと似たようなメカニズムで、ただ自分で実現したsimpleJdbcを使っています.今はibatisに基づいて細粒度キャッシュを実現する必要がありますが、そんなに簡単ではないようです.
 
=================================================
1.Voldemort(ヴォルデモート)
  • コンシステンシ
  • 挿抜可能
  • 独立
  • 強力なJavaクライアントで、挿抜可能なシーケンス化
  • をサポート
  • 統合可能
  • 2.ibatisはオブジェクトキャッシュを直接割り当てる方法がないにもかかわらず、プロジェクトはibatisを長い間使用していた(歴史的に問題が残っている)ので、ibatis+オブジェクトキャッシュ+クエリーキャッシュに改造しなければならないが、ibatis自体が何ができるかは期待できないので、SQL解析器として、戻ってきたオブジェクトをキャッシュする方法を考えましょう.オブジェクトに下位オブジェクトが含まれている処理にキーがあります.
    (1)ibatisの下位オブジェクトに対する処理は直接オブジェクトをインスタンス化して返すものであり,1対多(親子)関係の場合,戻る親オブジェクトには既にすべてのサブオブジェクトが含まれており,使い勝手がよく,キャッシュ可能な粒度が大きく,ほとんど制御できない.例えば、ツリー型組織機構のクエリーに基づいて、会社級、部門級、個人級の3級フィルタは3つのクエリーに分けられ、クエリーされた記録セットはどのようなものか想像することができ、大部分は重複しており、これらのオブジェクト全体をキャッシュに入れるのは、本当に奇妙だ.
    (2)理想的な方法は,子オブジェクトをインスタンス化して親オブジェクトに入れるのではなく,IDのlistを格納すべきである.セグメントコードはJDBCを例にとります.
     public MessageBean getByID(long id)
        throws MessageNotFoundException, DAOException
      {
            MessageBean bean = null;
    
            Connection con = getConnection();
            PreparedStatement pstmt = null;
            ResultSet rs = null;
            try
            {
            	// load message row
              pstmt = con.prepareStatement("SELECT pMessageID, ownerID, senderID, recipientID, subject, body, readStatus, folderID, pMessageDate, status, creationDate, sentDate FROM message WHERE pMessageID = ?");
              pstmt.setLong(1, id);
    
              rs = pstmt.executeQuery();
              if (!rs.next()) {
                throw new MessageNotFoundException("Message " + id + " could not be loaded from the database.");
              }
    
              bean = read(rs, new MessageBean());
    
              rs.close();
              pstmt.close();
    
            	// load message properties rows
              Map p = new HashMap();
              pstmt = con.prepareStatement("SELECT name, propValue FROM MessageProp WHERE pMessageID=?");
              pstmt.setLong(1, id);
              rs = pstmt.executeQuery();
              while (rs.next())
              {
                p.put(rs.getString(1), rs.getString(2));
              }
              bean.setProperties(p);
              rs.close();
              pstmt.close();
    
            	// load message attachment IDs
              LongList attachmentList = new LongList();
              try
              {
                pstmt = con.prepareStatement("SELECT attachmentID FROM attachment WHERE objectType=5 AND objectID=?");
                pstmt.setLong(1, bean.getID());
                rs = pstmt.executeQuery();
                while (rs.next()) {
                  attachmentList.add(rs.getLong(1));
                }
                bean.setAttachments(attachmentList);
              }
              catch (SQLException sqle) {
                Log.error(sqle);
              }
              finally {
                ConnectionManager.close(rs, pstmt);
              }
            }
            catch (SQLException sqle) {
              throw new MessageNotFoundException("Message with id " + id + " could not be loaded from the database.");
            }
            catch (NumberFormatException nfe)
            {
              throw new MessageNotFoundException("Message with id " + id + " could not be loaded from the database.");
            }
            finally
            {
              ConnectionManager.close(rs, pstmt);
              releaseConnection(con);
            }
    
            return bean; 
      }
    

      
    3.暗暗を乗り越え、なんとか伏地魔を制圧し、同時に応用する集団もあった.
    (3.1)voldemortプロファイル
    #!/bin/bash/voldemort-service.sh
    
    #
    #   Copyright 2008-2009 LinkedIn, Inc
    #
    #  Licensed under the Apache License, Version 2.0 (the "License");
    #   you may not use this file except in compliance with the License.
    #   You may obtain a copy of the License at
    #
    #      http://www.apache.org/licenses/LICENSE-2.0
    #
    #  Unless required by applicable law or agreed to in writing, software
    #  distributed under the License is distributed on an "AS IS" BASIS,
    #  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    #  See the License for the specific language governing permissions and
    #  limitations under the License.
    #
    
    if [ $# -gt 1 ];
    then
    	echo 'USAGE: bin/voldemort-server.sh [voldemort_home]'
    	exit 1
    fi
    
    base_dir=$(dirname $0)/..
    
    for file in $base_dir/dist/*.jar;
    do
      CLASSPATH=$CLASSPATH:$file
    done
    
    for file in $base_dir/lib/*.jar;
    do
      CLASSPATH=$CLASSPATH:$file
    done
    
    for file in $base_dir/contrib/*/lib/*.jar;
    do
      CLASSPATH=$CLASSPATH:$file
    done
    
    CLASSPATH=$CLASSPATH:$base_dir/dist/resources
    
    if [ -z "$VOLD_OPTS" ]; then
      VOLD_OPTS="\
    -Xmx2G -server \
    -Dcom.sun.management.jmxremote \
    -Dcom.sun.management.jmxremote.port=6650 \
    -Dcom.sun.management.jmxremote.ssl=false \
    -Dcom.sun.management.jmxremote.authenticate=false"
    fi
    
    java -Dlog4j.configuration=log4j.properties $VOLD_OPTS -cp $CLASSPATH voldemort.server.VoldemortServer $@
    

     
    メモリキャッシュの構成のみを永続化しない
    <!--cluster.xml-->
    <cluster>
    <name>pluscluster</name>
    <server>
        <id>0</id>
        <host>127.0.0.1</host>
        <http-port>8071</http-port>
        <socket-port>6666</socket-port>
        <admin-port>6667</admin-port>
        <partitions>0,1,2,3,4,5,6,7
        </partitions>
    </server>
    </cluster>
    

    server.properties
     
    # The ID of *this* particular cluster node
    node.id=0
    
    max.threads=100
    
    ############### DB options ######################
    
    http.enable=false
    socket.enable=true
    jmx.enable=true
    storage.configs=voldemort.store.bdb.BdbStorageConfiguration,voldemort.store.memory.InMemoryStorageConfiguration,voldemort.store.memory.CacheStorageConfiguration
    slop.store.engine=memory
    
    # NIO connector settings.
    enable.nio.connector=true
    enable.nio.admin.connector=false
    
    # don't throttle admin client - set to a really high value
    stream.read.byte.per.sec=1000000000000
    stream.write.byte.per.sec=1000000000000
    
    # default buffer size is 10MB. We'll turn it down to 512KB
    admin.streams.buffer.size=524288
    
    # use only 8 selectors (for nio)
    nio.connector.selectors=8
    nio.admin.connector.selectors=8
    

     
    4.アプリケーションアーキテクチャは共有Voldemortキャッシュに基づいて、JGroupを利用してメッセージを転送するアプリケーションクラスタは、1匹の布ほど長いので、しばらく省略して、数日おきに図を描きます.
    Arch-03-14-缓存策略_第1张图片
     
    5.JMXモニタの起動
     
        Jconsole     
     
    Arch-03-14-缓存策略_第2张图片
     
     
    6.キャッシュ・パフォーマンスの最適化方法
     
    (1)クライアントリソースキャッシュ
     
     
    HTTPD server - “cache.conf.rpmnew”

     
    (2)サービス側ページキャッシュ
     
     
    HTTP header in the response to Cache-Control max-age=3600.
     
     
    (3)外部CDNキャッシュサーバの構成
     
     
    .    CDN      
    .    URL
    .          ,           CDN URL
    .         ,      CDN URL     
    .    CDN   ,       ,  ,            CDN,  。

     
     
    Arch-03-14-缓存策略_第3张图片
     
     
    (4)仮想マシンパラメータの調整
     
     
    JVM_HEAP_MAX=4096
    JVM_HEAP_MIN=4096
     
     
    7.パクリCDNを作る
     
     
    原文を参考にするhttp://blog.csdn.net/jackem/article/details/3206440
    現在、CDNネットワーク構築技術には多くの成熟したビジネス案があり、資金が制限されているサイトに対しては自分で構築したり購入したりすることができない可能性があります.ここでは、CDNネットワークを簡単に実現するテクノロジーアーキテクチャを提供し、使用するソフトウェアはすべてオープンソースで効率的で無料です.CDNネットワーク技術の原理によると、bind、無料のオープンソースを使用できるオープンソースのダイナミックDNSサーバが必要です.コードを修正せずにbindは基本的に簡単な動的DNS解析機能を実現できる.コード実装を自分で修正できる、より強力なカスタム機能が必要です.
    (1)ウェブサイトが上海電信に配備されていると仮定する.
    (2)上海電信にBindを配置し、動的解析サービスを提供し、上海電信に主なWebサイトを配置する.
    (3)全国各地でいくつかの重要な機械室を探してlightpdサーバーを配置して静的資源のダウンロードを提供して、例えば北京網通、広州電信の2つの機械室はそれぞれ2台のサーバーを置いて、lightpd+modをインストールしますcache+mod_proxy等;
    (4)サブダウンロードサーバのlighttpdのmodCache機能を構成しproxyを設定する.serverメインサイト上海機械室IP.
    (5)北京網通ユーザーがメインステーションの静的ファイルにアクセスすると、動的DNS解析により北京網通機室サーバーのIPアドレスが得られ、ブラウザは北京網通サーバーからファイルをダウンロードし、応答速度を高め、同時にメインステーションの圧力を低減した.
    このような簡単なテクノロジーアーキテクチャが完了しました.ライトtpd+modCacheの詳細構成リファレンスhttp://www.linux.com.cn/modcache/.
     
     
    Arch-03-14-缓存策略_第4张图片
     
    
    
        <bean id="usersCache" parent="cacheBean" depends-on="cacheFactoryManager">
            <constructor-arg value="Users" />
            <property name="type" ref="local"/>
            <!-- in milliseconds, 24 hours, checked every hour -->
            <property name="expirationTime" value="86400000"/>
            <property name="expirationPeriod" value="3600000"/>
            <property name="transactional" value="false"/>
            <property name="keyRetrieval" value="false"/>
            <property name="type" value="false"/>
            <property name="timeoutTime" value="60"/>
            <property name="timeUnit" value="false"/>
            <property name="additionalProperties" value="Map<String, String>"/>
            <property name="valueSerializer" value="false"/>
            <property name="disableEviction" value="false"/>
            <property name="prefixAware" value="false"/>
            <property name="maxCacheSize" value="10g"/>
            <sizeLimit
        </bean>