سؤال

لقد حصلت على طريقة عرض في Django تستخدم Memcached لتخزين بيانات Cache للآراء الأكثر تهريبًا والتي تعتمد على مجموعة ثابتة نسبيًا من البيانات. الكلمة الرئيسية نسبيًا: أحتاج إلى إبطال مفتاح memcached لبيانات عنوان URL هذا عندما يتم تغييره في قاعدة البيانات. لكي تكون واضحًا قدر الإمكان ، إليك اللحوم "البطاطس من العرض" (الشخص هو نموذج ، ذاكرة التخزين المؤقت هي django.cache.cache):

def person_detail(request, slug): 
    if request.is_ajax():
        cache_key = "%s_ABOUT_%s" % settings.SITE_PREFIX, slug

        # Check the cache to see if we've already got this result made.
        json_dict = cache.get(cache_key)

        # Was it a cache hit?
        if json_dict is None:
            # That's a negative Ghost Rider
            person = get_object_or_404(Person, display = True, slug = slug)

            json_dict = {
                'name' : person.name,
                'bio' : person.bio_html,
                'image' : person.image.extra_thumbnails['large'].absolute_url,
            }

            cache.set(cache_key)

        # json_dict will now exist, whether it's from the cache or not
        response = HttpResponse()
        response['Content-Type'] = 'text/javascript'
        response.write(simpljson.dumps(json_dict)) # Make sure it's all properly formatted for JS by using simplejson
        return response
    else:
        # This is where the fully templated response is generated

ما أريد القيام به هو الحصول على هذا المتغير Cache_Key في نموذج "غير مهتم" ، لكنني لست متأكدًا من كيفية القيام بذلك-إذا كان يمكن القيام به على الإطلاق.

فقط في حالة وجود شيء ما للقيام بذلك بالفعل ، إليك ما أريد أن أفعله به (هذا من طريقة حفظ نموذج الشخص الافتراضي)

def save(self):    
    # If this is an update, the key will be cached, otherwise it won't, let's see if we can't find me
    try:
        old_self = Person.objects.get(pk=self.id)
        cache_key = # Voodoo magic to get that variable
        old_key = cache_key.format(settings.SITE_PREFIX, old_self.slug) # Generate the key currently cached
        cache.delete(old_key) # Hit it with both barrels of rock salt

    # Turns out this  doesn't already exist, let's make that first request even faster by making this cache right now
    except DoesNotExist:
        # I haven't gotten to this yet.

    super(Person, self).save()

أفكر في جعل فئة عرض لهذه الأشياء نوعًا remove_cache أو generate_cache منذ أن أفعل هذا النوع من الأشياء كثير. هل ستكون هذه فكرة أفضل؟ إذا كان الأمر كذلك ، كيف يمكنني الاتصال بالآراء في urlconf إذا كانت في فصل ما؟

هل كانت مفيدة؟

المحلول

يجب أن يشير UrlConf إلى أي قابلة للاستدعاء. لا يوجد أي شرط صارم لجعله يشير إلى العمل بالضبط. يمكنك تنفيذ فئة قاعدة مع أساليب ذاكرة التخزين المؤقت الخاصة بك ثم تمديدها:

class RealView(BaseViewWithCacheMethods):
    def __call__(self, request):
        if request.is_ajax():
            return self.ajax_view()
        return self.html_view()

سيكون تعريف URLCONF شيئًا من هذا القبيل:

from django.conf.urls.defaults import *
from views import RealView

urlpattrens = patterns('',
    (r'^$', RealView()),
)
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top