from techism2.models import Event
from datetime import datetime


def get_tags():
    # TODO: cache, use django cache which uses GAE memcache
    dict_list = Event.objects.values('tags')
    tags = dict()    
    for dictionary in dict_list:
        for tag_list in dictionary.itervalues():
            if tag_list:
                for tag in tag_list:
                    if tag not in tags:
                        tags[tag] = 0
                    tags[tag] += 1
    return tags


from techism2.models import Event
from datetime import datetime
from django.core.cache import cache

tags_cache_key = "tags"

def get_tags():
    # Note: no synchronization, propably not possible on GAE
    tags = cache.get(tags_cache_key)
    
    if tags:
        return tags
    else:
        tags = __fetch_tags()
        cache.set(tags_cache_key, tags, 1800) # expire after 30 min
        return tags

def __fetch_tags():
    dict_list = Event.objects.values('tags')
    tags = dict()    
    for dictionary in dict_list:
        for tag_list in dictionary.itervalues():
            if tag_list:
                for tag in tag_list:
                    if tag not in tags:
                        tags[tag] = 0
                    tags[tag] += 1
    return tags


Use Memcache to cache tags
Use Memcache to cache tags

  from techism2.models import Event
  from datetime import datetime
+ from django.core.cache import cache
  
+ tags_cache_key = "tags"
  
  def get_tags():
-     # TODO: cache, use django cache which uses GAE memcache
+     # Note: no synchronization, propably not possible on GAE
+     tags = cache.get(tags_cache_key)
+     
+     if tags:
+         return tags
+     else:
+         tags = __fetch_tags()
+         cache.set(tags_cache_key, tags, 1800) # expire after 30 min
+         return tags
+ 
+ def __fetch_tags():
      dict_list = Event.objects.values('tags')
      tags = dict()    
      for dictionary in dict_list:
          for tag_list in dictionary.itervalues():
              if tag_list:
                  for tag in tag_list:
                      if tag not in tags:
                          tags[tag] = 0
                      tags[tag] += 1
      return tags
  
  
Use Memcache to cache tags
## Code Before:
from techism2.models import Event
from datetime import datetime


def get_tags():
    # TODO: cache, use django cache which uses GAE memcache
    dict_list = Event.objects.values('tags')
    tags = dict()    
    for dictionary in dict_list:
        for tag_list in dictionary.itervalues():
            if tag_list:
                for tag in tag_list:
                    if tag not in tags:
                        tags[tag] = 0
                    tags[tag] += 1
    return tags


## Instruction:
Use Memcache to cache tags
## Code After:
from techism2.models import Event
from datetime import datetime
from django.core.cache import cache

tags_cache_key = "tags"

def get_tags():
    # Note: no synchronization, propably not possible on GAE
    tags = cache.get(tags_cache_key)
    
    if tags:
        return tags
    else:
        tags = __fetch_tags()
        cache.set(tags_cache_key, tags, 1800) # expire after 30 min
        return tags

def __fetch_tags():
    dict_list = Event.objects.values('tags')
    tags = dict()    
    for dictionary in dict_list:
        for tag_list in dictionary.itervalues():
            if tag_list:
                for tag in tag_list:
                    if tag not in tags:
                        tags[tag] = 0
                    tags[tag] += 1
    return tags

