2009-11-15 7 views
3

누구나 웹 응용 프로그램에서이 조합을 사용합니까? 이 문제를 구성하기위한 가이드 또는 지침을 찾는 데 어려움을 겪고 있습니다. 또한 내가 어떻게 Pylons를 사용하기 시작했는지에 관해서는 최근 익숙하지 않기 때문에 조언을 매우 초보자들에게 친절하게 (나는 심지어 비커와 같은 모듈을 사용하지도 않았다.파일런과 Memcached

MySQL을 사용하고 있습니다. pastie HTTP 서버를 실행하고 있습니다. 데비안에 memcached 패키지를 설치 했으므로 SQLAlchemy ORM을 사용하여 Pylons 앱에서 내 DB와 상호 작용할 수 있습니다. 이제 어떻게해야할지 모르겠습니다.

답변

7

memcached은 프레임 워크에 전혀 영향을받지 않으며 훌륭하게 작동하기 때문에 약간의 코드 만 작성하면됩니다. memcached의 일반적인 아이디어는 다음과 같습니다.

object = try_memcached() 
if not object: 
    object = real_query() 
    put_in_memcached(object) 

아마도 SQLAlchemy 추상화에서 수행 될 것입니다. 내가 전체 플랫폼에 익숙하지 않기 때문에 (그리고 memcached 만), 나는 약간의 인터넷 검색을 수행했습니다.

This blogger은 함께 구현 한 것으로 보이며 그가 사용하는 코드에 대한 링크를 제공했습니다. 관련 코드가 다음과 같이 나타납니다. 귀하에게 의미가있을 수 있습니다.

#!/usr/bin/python 
""" 
memcached objects for use with SQLAlchemy 
""" 
# This program is free software: you can redistribute it and/or modify 
# it under the terms of the GNU General Public License as published by 
# the Free Software Foundation, either version 3 of the License, or 
# (at your option) any later version. 
# 
# This program is distributed in the hope that it will be useful, 
# but WITHOUT ANY WARRANTY; without even the implied warranty of 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
# GNU General Public License for more details. 
# 
# You should have received a copy of the GNU General Public License 
# along with this program. If not, see <http://www.gnu.org/licenses/>. 

import memcache 
import sqlalchemy 
import sqlalchemy.orm 

SQLA_SESSION = sqlalchemy.orm.sessionmaker() 

MEMCACHED_CLIENT = memcache.Client(['127.0.0.1:11211']) 

class DetachedORMObject(object): 
    """ 
    Session-detached object for use with ORM Mapping. As the SQLAlchemy 
    documentation indicates, creating and closing a session is not analogous 
    to creating and closing a database connection. Connections are pooled by 
    the database engine by default. Creating new sessions is of a minimal 
    cost. Also, objects using this wrapper will not likely interact in with 
    the database through the full power of SQLAlchemy queries. 

    """ 
    @classmethod 
    def fetch_by_field(cls, field, value): 
     """Fetch a mapped orm object with the give field and value""" 
     session = SQLA_SESSION() 
     try: 
      class_object = session.query(cls).filter(field == value).one() 
     except sqlalchemy.orm.exc.NoResultFound: 
      class_object = None 
     finally: 
      session.close() 
     return class_object 

    def update(self): 
     """Update the database with the values of the object""" 
     session = SQLA_SESSION() 
     session.add(self) 
     session.commit() 
     session.refresh(self) 
     session.close() 

    def refresh(self): 
     """Refresh the object with the values of the database""" 
     session = SQLA_SESSION() 
     session.add(self) 
     session.refresh(self) 
     session.close() 

    def delete(self): 
     """Delete the object from the database""" 
     session = SQLA_SESSION() 
     session.add(self) 
     session.delete(self) 
     session.commit() 
     session.close() 


class MemcachedObject(object): 
    """ 
    Object Wrapper for serializing objects in memcached. Utilizes an abstract 
    method, get_isntance_key, to understand how to get and set objects that 
    impliment this class. 
    """ 
    @classmethod 
    def get_cached_instance(cls, instance_key): 
     """Retrieve and return the object matching the instance_key""" 
     key = str(cls.__module__ + '.' + cls.__name__ + ':' \ 
      + str(instance_key)) 
     print "Memcached Getting:", key 
     return MEMCACHED_CLIENT.get(key) 

    def set_cached_instance(self, time=0, min_compress_len=0): 
     """Set the cached instance of an object""" 
     print "Memcached Setting:", self.get_cache_key() 
     return MEMCACHED_CLIENT.set(self.get_cache_key(), self, time, \ 
      min_compress_len) 

    def delete_cached_instance(self, time=0): 
     """Wrapper for the memcached delete method""" 
     print "Memcached Deleting:", self.get_cache_key() 
     return MEMCACHED_CLIENT.delete(self.get_cache_key(), time) 

    def get_cache_key(self): 
     """Prepends the full class path of the object to the instance key""" 
     return self.__class__.__module__ + '.' + \ 
      self.__class__.__name__ + ':' + self.get_instance_key() 

    def get_instance_key(self): 
     """Get the instance key, must be implemented by child objects""" 
     raise NotImplementedError \ 
      ("'GetInstanceKey' method has not been defined.") 


class MemcachedORMObject(DetachedORMObject, MemcachedObject): 
    """ 
    Putting it all together now. Implements both of the above classes. Worth 
    noting is the method for checking to see if the fetch_by_field method is 
    invoked using a primary key of the class. The same technique is used to 
    generate an instance key for an instance of the class. 
    """ 
    @classmethod 
    def fetch_by_field(cls, field, value): 
     """Fetch the requested object from the cache and database""" 
     orm_object = None 
     matched_primary_key = True 
     for key in cls._sa_class_manager.mapper.primary_key: 
      if field.key != key.key: 
       matched_primary_key = False 
     if matched_primary_key: 
      orm_object = cls.get_cached_instance('(' + str(value) + ')') 
     if orm_object is None: 
      orm_object = super(MemcachedORMObject, cls). \ 
       fetch_by_field(field, value) 
      if orm_object is not None: 
       orm_object.set_cached_instance() 
     return orm_object 

    def update(self): 
     """Update the object in the database and memcached""" 
     DetachedORMObject.update(self) 
     self.set_cached_instance() 

    def refresh(self): 
     """Refresh the object from the database and memcached""" 
     DetachedORMObject.refresh(self) 
     self.set_cached_instance() 

    def delete(self): 
     """Delete the object from the database and memcached""" 
     DetachedORMObject.delete(self) 
     self.delete_cached_instance() 

    def get_instance_key(self): 
     """Get the instance key, implimenting abstract method in base""" 
     key = [] 
     for column in self._sa_instance_state.manager.mapper.primary_key: 
      key.append('(' + str(getattr(self, column.key)) + ')') 
     return ''.join(key) 

도움이 될지 모르지만 확실하게 알고 있습니다. 당신이 사용하는 memcached 관용구를 볼 수 있습니다

if matched_primary_key: 
     orm_object = cls.get_cached_instance('(' + str(value) + ')') 
    if orm_object is None: 
     orm_object = super(MemcachedORMObject, cls). \ 
      fetch_by_field(field, value) 
1

철탑은 캐싱에 비커를 권장하고 그것은 Memcache의 백엔드 있습니다. here

을 참조하십시오.