def get_myself_data(self, userid, fields=None, caching=True, cache_update=None): """ アプリを利用しているユーザー本人の情報を取得する 任意でキャッシュできる(デフォルトではキャッシュする) Get the information of user. You can use caching(default will do it) """ path = '/people/%s/@self' % userid cache_key = path data = None if caching: data = GsocialCache.get_cache(cache_key) #print 'cache:', data # dataが空の時、もしくは、キャッシュを更新したい時 if data == None or cache_update == True: #print 'not cache:', data #print 'cache_update:', cache_update params = {} if fields: params['fields'] = fields res = self.get_response(userid, path, params) Log.debug('Debug.', res) if res: try: data = simplejson.loads(res) if caching: GsocialCache.set_cache(cache_key, data) except TypeError: Log.error(self.LOG_MSG2, [userid, path, res]) try: data = simplejson.loads(unicode( res, "utf-8", "ignore")) if caching: GsocialCache.set_cache(cache_key, data) except TypeError: data = None Log.error(self.LOG_MSG3, [userid, path, res]) except Exception: data = None Log.error(self.LOG_MSG1, [userid, path, res]) except Exception: data = None Log.error(self.LOG_MSG1, [userid, path, res]) else: data = None Log.error(self.LOG_MSG4, [userid, path]) return data
def update_record(self, record, **argv): """ 認証レコード更新 """ for name, value in argv.iteritems(): if name in self.UPDATABLE_COLUMNS and hasattr(record, name): setattr(record, name, value) record.save() GsocialCache.set_cache(self.cache_key, None, 1) # キャッシュ削除
def test_get_cache(self): cache_key = self.CACHE_KEY cache_value = self.CACHE_VALUE1 # セットする GsocialCache.set_cache(cache_key, cache_value, timeout=5) # 取得する result = GsocialCache.get_cache(cache_key) eq_('test_value1', result)
def get_myself_data(self, userid, fields=None, caching=True, cache_update=None): """ アプリを利用しているユーザー本人の情報を取得する 任意でキャッシュできる(デフォルトではキャッシュする) Get the information of user. You can use caching(default will do it) """ path = '/people/%s/@self' % userid cache_key = path data = None if caching: data = GsocialCache.get_cache(cache_key) #print 'cache:', data # dataが空の時、もしくは、キャッシュを更新したい時 if data == None or cache_update == True: #print 'not cache:', data #print 'cache_update:', cache_update params = {} if fields: params['fields'] = fields res = self.get_response(userid, path, params) Log.debug('Debug.', res) if res: try: data = simplejson.loads(res) if caching: GsocialCache.set_cache(cache_key, data) except TypeError: Log.error(self.LOG_MSG2, [userid, path, res]) try: data = simplejson.loads(unicode(res, "utf-8", "ignore")) if caching: GsocialCache.set_cache(cache_key, data) except TypeError: data = None Log.error(self.LOG_MSG3, [userid, path, res]) except Exception: data = None Log.error(self.LOG_MSG1, [userid, path, res]) except Exception: data = None Log.error(self.LOG_MSG1, [userid, path, res]) else: data = None Log.error(self.LOG_MSG4, [userid, path]) return data
def create_record(self, auth_id, is_authorized = False, user_grade=None): """ 認証レコード作成 作成したAuthDeviceモデルインスタンスを返す """ Log.debug('Check is_authorized.', is_authorized) auth = AuthDevice.objects.partition(self.user_id).create( osuser_id=self.user_id, auth_id=auth_id, user_grade=user_grade, is_authorized=is_authorized) GsocialCache.set_cache(self.cache_key, None, 1) # キャッシュ削除 return auth
def get_auth_id(self): """ APIによる 識別ID取得 キャッシュ制御も行う """ auth_id = GsocialCache.get_cache(self.cache_key) if auth_id is None: auth_id = self._get_auth_id(self.user_id) if auth_id: GsocialCache.set_cache(self.cache_key, auth_id, self.CACHE_TIMEOUT) return auth_id if auth_id else None
def get_record(self): """ 認証レコード取得 AuthDeviceモデルインスタンスを返す """ auth = GsocialCache.get_cache(self.cache_key) if auth is None: try: auth = AuthDevice.objects.partition(self.user_id).get(osuser_id=self.user_id) if AuthDeviceManager.check_user_grade(auth.user_grade): timeout = self.CACHE_TIMEOUT GsocialCache.set_cache(self.cache_key, auth, timeout) # グレード 3以下はキャッシュしない # 従来は 3600秒キャッシュしていた except AuthDevice.DoesNotExist: auth = None return auth
def put(self, userid, text_id, message): ''' PUT(更新) POSTで得られたitemIdを用いることでテキストデータを更新することができる PUT(update) You can update the text data using the itemId you got from POST. ''' # ローカルではリクエストできない # You can`t request on local. if settings.OPENSOCIAL_DEBUG: return None message = self.container.encode_emoji(message) data = simplejson.dumps({'data': message}) response = self._api_request('PUT', userid, text_id = str(text_id), data = data) Log.debug('Inspection PUT. response: %s' % (response)) # GET のために削除 # Delete for GET GsocialCache.delete_cache(self._get_cache_key(text_id)) return None
def delete(self, userid, text_id): ''' DELETE(削除) item_idはPOSTで得られたitemId DELETE item_id is the itemId you got from POST. ''' # ローカルではリクエストできない # You can`t request on local. if settings.OPENSOCIAL_DEBUG: return None response = self._api_request('DELETE', userid, text_id = text_id) Log.debug('Inspection DELETE. response: %s' % (response)) # 削除 # delete GsocialCache.delete_cache(self._get_cache_key(text_id)) # test #cache =GsocialCache.get_cache(self._get_cache_key(text_id)) #print 'delete cache', cache return None
def _cache_clear(cls, user_id): """ cache_clear """ auth_model = '/opensocial/authdevice/model/%s' % user_id user_grade = '/opensocial/authdevice/api/user_grade/%s' % user_id auth_id = '/opensocial/authdevice/api/%s' % user_id GsocialCache.delete_cache(auth_model) GsocialCache.delete_cache(user_grade) GsocialCache.delete_cache(auth_id)
def get_auth(self): """ APIによる 識別ID/UserGrade取得 キャッシュ制御も行う """ #cache.set(self.user_grade_cache_key, None, 1) user_grade = GsocialCache.get_cache(self.user_grade_cache_key) auth_id = GsocialCache.get_cache(self.cache_key) if user_grade is None: auth_id, user_grade = self._get_auth(self.user_id) if user_grade: GsocialCache.set_cache(self.user_grade_cache_key, user_grade, self.CACHE_TIMEOUT) else: user_grade = None if auth_id: GsocialCache.set_cache(self.cache_key, auth_id, self.CACHE_TIMEOUT) return [auth_id, user_grade]
def blacklist_check(self, userid, target_userid, caching=False, cache_update=None): """ 二者間でのブラックリストチェック userid が target_userid をブラックリスト登録してるか? 任意でキャッシュできる(デフォルトではキャッシュしない) (GREE規約上、ブラックリストは24時間キャッシュできるようだが、しない) 引数 userid target_userid caching cache_update 返り値 is_data : Trueなら、ブラックリストに登録している Falseなら、ブラックリストに登録されていない "Blacklist checking" between the two persons:whether the "userid" has registered "target_userid" for blacklist? You can cache it if you want(but by default you can`t cache it) arguments userid target_userid caching cache_update return value is_data : if True,registerd on blacklist if False,not registerd on blacklist """ from django.conf import settings if settings.OPENSOCIAL_DEBUG: return False #requestor_id = self.container.request.osuser.userid path = '/ignorelist/%s/@all/%s/' % (str(userid), str(target_userid)) cache_key = path data = [] # test #cache_update = True if caching and cache_update == False: is_data = GsocialCache.get_cache(cache_key) #print 'cache', is_data if is_data != None: return is_data for retry_count in xrange(self.RETRY_COUNT): try: res = self.get_response(userid, path) #res = self.get_response(requestor_id, path) data = simplejson.loads(res) break except TypeError: Log.warn('Blacklist: response type error. retry:%s' % retry_count) continue except Exception: Log.warn('Blacklist: response error. retry:%s' % retry_count) continue Log.debug(data) if not data: Log.error("Blacklist: not data.") raise self.container.ResponseError('Blacklist API', 'Get ignorelist (gree).') if 'entry' in data and data['entry']: is_data = True else: is_data = False #print 'no cache', is_data if caching: GsocialCache.set_cache(cache_key, is_data) return is_data
def test_set_cache(self): cache_key = self.CACHE_KEY cache_value = self.CACHE_VALUE1 GsocialCache.set_cache(cache_key, cache_value, timeout=5)
def blacklist_check(self, userid, target_userid, caching=False, cache_update=None): """ 二者間でのブラックリストチェック userid が target_userid をブラックリスト登録してるか? 任意でキャッシュできる(デフォルトではキャッシュしない) (GREE規約上、ブラックリストは24時間キャッシュできるようだが、しない) 引数 userid target_userid caching cache_update 返り値 is_data : Trueなら、ブラックリストに登録している Falseなら、ブラックリストに登録されていない "Blacklist checking" between the two persons:whether the "userid" has registered "target_userid" for blacklist? You can cache it if you want(but by default you can`t cache it) arguments userid target_userid caching cache_update return value is_data : if True,registerd on blacklist if False,not registerd on blacklist """ from django.conf import settings if settings.OPENSOCIAL_DEBUG: return False #requestor_id = self.container.request.osuser.userid path = '/ignorelist/%s/@all/%s/' % (str(userid), str(target_userid)) cache_key = path data = [] # test #cache_update = True if caching and cache_update == False: is_data = GsocialCache.get_cache(cache_key) #print 'cache', is_data if is_data != None: return is_data for retry_count in xrange(self.RETRY_COUNT): try: res = self.get_response(userid, path) #res = self.get_response(requestor_id, path) data = simplejson.loads(res) break except TypeError: Log.warn('Blacklist: response type error. retry:%s' % retry_count) continue except Exception: Log.warn('Blacklist: response error. retry:%s' % retry_count) continue Log.debug(data) if not data: Log.error("Blacklist: not data.") raise self.container.ResponseError('Blacklist API','Get ignorelist (gree).') if 'entry' in data and data['entry']: is_data = True else: is_data = False #print 'no cache', is_data if caching: GsocialCache.set_cache(cache_key, is_data) return is_data
def gets(self, userid, text_ids, caching = True, retry_count = None): ''' 複数ID指定のGET(取得) text_ids は、listのような複数のIDをいれたiterableなもの GET with Multi-IDs. text_ids is a iterable thing that contain multiple IDs(list). ''' # ローカルではリクエストできない # You can`t request on local if settings.OPENSOCIAL_DEBUG: return [] retry_count = retry_count if retry_count else self.RETRY_COUNT get_text_ids = [] result_list = [] for text_id in text_ids: # 一つずつキャッシュを確認する # Confirms cache each by each. cache_key = self._get_cache_key(text_id) id_data_entry = None if caching: id_data_entry = GsocialCache.get_cache(cache_key) #print 'cache:', id_data_entry if id_data_entry is None: #print 'not cache:', id_data_entry get_text_ids.append(text_id) id_data_entry = (text_id, None, None) # None の場合は None で構わない # None doesn`t necessary indicate the problems is underlying. result_list.append(id_data_entry) # 取得すべきIDだけとりにいく(すべてある場合はキャッシュで解決する筈) # You get the ID you need(caching might solve if all of them are already there) if len(get_text_ids) > 0: get_entry_dict = {} text_ids = ','.join(get_text_ids) response = self._api_request('GET', userid, text_id = text_ids) Log.debug('response: %s' % response) if response != None: json = simplejson.loads(response) if 'entry' in json: entry_list = json['entry'] for entry in entry_list: if 'data' in entry: data = entry['data'] else: data = u'' data = self.container.decode_emoji(data) if caching: text_id = entry['textId'] cache_key = self._get_cache_key(text_id) status = int(entry['status']) if status == self.STATUS_INSPECTING: # 検査中 # 監査中の場合、3時間までキャッシュ可能 timeout = 3 * 60 * 60 elif status == self.STATUS_OK: # 監査OK # 監査OKの場合、24時間までキャッシュ可能 timeout = 24 * 60 * 60 elif status == self.STATUS_NG: # 監査NG # 監査NGの場合、無期限でキャッシュ可能だが、24時間 timeout = 24 * 60 * 60 else: timeout = None if timeout: # textId 毎のキャッシュを必ずつくる GsocialCache.set_cache(cache_key, (text_id, data, entry), timeout=timeout) Log.debug('Inspection Cache Set status: %d(%d).' % (status, timeout)) # リストの該当箇所に突っ込む get_entry_dict[text_id] = (text_id, data, entry) new_result_list = [] for result in result_list: if result[0] in get_entry_dict: result = get_entry_dict[result[0]] new_result_list.append(result) result_list = new_result_list return result_list