1

Grails と Groovy を使用して、大規模な保険契約と請求管理システムを構築しました。すべての「READS」がデータベースからフェッチされるため、パフォーマンスの問題によってサイトの速度が低下していますが、ほとんどのデータは静的であるため、これは必要ありません。Grails レイヤーに単純なキー/値キャッシュを導入したいのですが、既存のコードを cache.get() および cache.set() コードで散らかしたくないので、代わりにアスペクトを使用したいと考えています。

これがメインコントローラーのサンプルです....

InsuranceMainController {
  def customer {
    //handles all URI mappings for /customer/customerId
  }

  def policy {
    //handles all URI mappings for /policy/policyId, 
  }

  def claim {
    //handles all URL mappings for /claim/claimId
  }

キャッシュに関する限り、当面はグローバルスコープのオブジェクトとして利用可能な「キャッシュ」という名前の単純なマップであり、キャッシュ内のオブジェクトはリクエスト URI によってキー付けされると仮定します...

cache.put("/customer/99876", customerObject)
cache.put("/policy/99-33-ARYT", policyObject) 

コントローラーに戻ると、Spring AOP を使用して回避したい cache.get()/set() でコードを散らかすだけでは、厄介なコードになってしまいます。私たちは、apsects を使用して、またはよりシンプルでクリーンな実装で次の機能を実現したいと考えています...

InsuranceMainController {
  def customer {

    Object customer = cache.get(request.getRequestURI())
    if ( customer != null)
       //render response with customer object
    }else
       //get the customer from the database, then add to cache
       CustomerPersistenceManager customerPM = ...
       customer = customerPM.getCustomer(customerId)
       cache.put(request.getRequestURI(), customer)
    }
  }

cache.get()/set() によるコードの散らかりを回避しながら、Spring AOP または Grails でより単純なものを使用して上記の機能を実現する方法を示す例が必要です。AOP を適切に動作させるために必要な場合は、既存のコントローラーをリファクタリングする提案を歓迎します。

前もって感謝します

4

1 に答える 1

2

AOP を使用する代わりに、Paul Woods 氏のコントローラー単純化パターンを適用して、キャッシュ処理を単一のメソッドに移動できますか?

このようなものがうまくいくかもしれません:

class InsuranceMainController {

  def customer = {
    Object customer = withCachedRef( 'customerId' ) { customerId ->
       CustomerPersistenceManager customerPM = ...
       customerPM.getCustomer(customerId)
    }
  }

  def policy = {
    //handles all URI mappings for /policy/policyId, 
    Object policy = withCachedRef( 'policyId' ) { policyId ->
       PolicyPersistenceManager policyPM = ...
       policyPM.getPolicy(policyId)
    }
  }

  // ...

  private def withCachedRef( String id, Closure c ) {
    Object ret = cache.get( request.requestURI )
    if( !ret ) {
      ret = c.call( params[ id ] )
      cache.put( request.requestURI, ret )
    }
    ret
  }
}

ただし、まったくテストしていません:-( AOPの代替案の提案にすぎません

于 2011-12-05T16:15:21.397 に答える