Exemplo n.º 1
0
 def get(self):
     date = datetime.datetime.now() - datetime.timedelta(days=1)
     search_cache_query = SearchCache().all()
     search_cache_query.filter('tweeted_at <', date)
     search_cache_query.order('tweeted_at')
     search_cache = search_cache_query.fetch(100)
     
     logging.info('Delete old cache. count: %d' % len(search_cache))
     
     for cache in search_cache:
         cache.delete()
Exemplo n.º 2
0
    def get(self):
        date = datetime.datetime.now() - datetime.timedelta(days=1)
        search_cache_query = SearchCache().all()
        search_cache_query.filter('tweeted_at <', date)
        search_cache_query.order('tweeted_at')
        search_cache = search_cache_query.fetch(100)

        logging.info('Delete old cache. count: %d' % len(search_cache))

        for cache in search_cache:
            cache.delete()
Exemplo n.º 3
0
 def get(self):
     import random
     from django.utils import simplejson
     
     cache_ids = memcache.get('cache_ids')
     if cache_ids is None:
         search_cache_query = SearchCache().all()
         search_cache_query.order('-tweeted_at')
         search_cache = search_cache_query.fetch(200)
         
         cache_ids = []
         for tweet in search_cache:
             cache_ids.append(tweet.key().id())
         
         memcache.Client().add('cache_ids', cache_ids, 60)
         logging.info('cache_ids from datastore.')
     else:
         logging.info('cache_ids from memcache.')
     
     if len(cache_ids) > 0:
         cache_id = cache_ids[random.randint(0, len(cache_ids))]
         logging.info('Random cache_id: %d' % cache_id)
         
         data = memcache.get('cache_%d' % cache_id)
         if data is None:
             logging.info('Tweet cache from datastore.')
             search_cache = SearchCache().get_by_id(int(cache_id))
             if search_cache is not None:
                 delta = datetime.datetime.now() - search_cache.tweeted_at
                 if delta.seconds < 120:
                     delta_str = int(delta.seconds)
                 else:
                     delta_str = '-'
                     
                 if len(search_cache.text) < 60:
                     data = {'id': search_cache.key().id(),
                             'name': search_cache.name,
                             'screen_name': search_cache.screen_name,
                             'text': search_cache.text,
                             'profile_image_url': search_cache.profile_image_url,
                             'delta':delta_str,
                             'created_at': search_cache.tweeted_at.strftime('%a %b %d %H:%M:%S +0000 %Y')}
                     
                     memcache.Client().add('cache_%d' % cache_id, data, 3600)
                     
                     json = simplejson.dumps(data, ensure_ascii=False)
                     self.response.content_type = 'application/json'
                     self.response.out.write(json)
         else:
             logging.info('Tweet cache from memcache.')
             json = simplejson.dumps(data, ensure_ascii=False)
             self.response.content_type = 'application/json'
             self.response.out.write(json)
Exemplo n.º 4
0
    def get(self):
        search_cache_query = SearchCache().all()
        search_cache_query.order('-tweeted_at')
        search_cache = search_cache_query.fetch(200)

        cache_ids = []
        for tweet in search_cache:
            cache_ids.append(tweet.key().id())

        if memcache.get('cache_ids') is not None:
            memcache.Client().replace('cache_ids', cache_ids, 300)
        else:
            memcache.Client().add('cache_ids', cache_ids, 300)

        logging.info('Updated cache_ids.')
Exemplo n.º 5
0
 def get(self):
     search_cache_query = SearchCache().all()
     search_cache_query.order('-tweeted_at')
     search_cache = search_cache_query.fetch(200)
     
     cache_ids = []
     for tweet in search_cache:
         cache_ids.append(tweet.key().id())
     
     if memcache.get('cache_ids') is not None:
         memcache.Client().replace('cache_ids', cache_ids, 300)
     else:
         memcache.Client().add('cache_ids', cache_ids, 300)
         
     logging.info('Updated cache_ids.')
Exemplo n.º 6
0
    def get(self):
        import random
        from django.utils import simplejson

        cache_ids = memcache.get('cache_ids')
        if cache_ids is None:
            search_cache_query = SearchCache().all()
            search_cache_query.order('-tweeted_at')
            search_cache = search_cache_query.fetch(100)

            cache_ids = []
            for tweet in search_cache:
                cache_ids.append(tweet.key().id())

            memcache.Client().add('cache_ids', cache_ids, 300)
            logging.info('cache_ids from datastore.')
        else:
            logging.info('cache_ids from memcache.')

        if len(cache_ids) > 0:
            cache_id = cache_ids[random.randint(0, len(cache_ids))]
            logging.info('Random cache_id: %d' % cache_id)

            data = memcache.get('cache_%d' % cache_id)
            if data is None:
                logging.info('Tweet cache from datastore.')
                search_cache = SearchCache().get_by_id(int(cache_id))
                if search_cache is not None:
                    delta = datetime.datetime.now() - search_cache.tweeted_at
                    if delta.seconds < 120:
                        delta_str = int(delta.seconds)
                    else:
                        delta_str = '-'

                    created_at = search_cache.tweeted_at + datetime.timedelta(
                        hours=9)

                    if search_cache.time_zone:
                        time_zone = search_cache.time_zone
                    else:
                        time_zone = ''

                    if len(search_cache.text) < 60:
                        data = {
                            'id':
                            search_cache.key().id(),
                            'name':
                            search_cache.name,
                            'screen_name':
                            search_cache.screen_name,
                            'text':
                            search_cache.text,
                            'profile_image_url':
                            search_cache.profile_image_url,
                            'delta':
                            delta_str,
                            'created_at':
                            created_at.strftime('%Y-%m-%d %H:%M:%S (JST)'),
                            'time_zone':
                            time_zone
                        }

                        memcache.Client().add('cache_%d' % cache_id, data,
                                              3600)

                        json = simplejson.dumps(data, ensure_ascii=False)
                        self.response.content_type = 'application/json'
                        self.response.out.write(json)
            else:
                logging.info('Tweet cache from memcache.')
                json = simplejson.dumps(data, ensure_ascii=False)
                self.response.content_type = 'application/json'
                self.response.out.write(json)