Page Speed Optimization Libraries
1.7.30.2
|
00001 /* 00002 * Copyright 2013 Google Inc. 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http:///www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 00021 00022 #ifndef NET_INSTAWEB_REWRITER_PUBLIC_PROPERTY_CACHE_UTIL_H_ 00023 #define NET_INSTAWEB_REWRITER_PUBLIC_PROPERTY_CACHE_UTIL_H_ 00024 00025 #include "net/instaweb/rewriter/public/rewrite_driver.h" 00026 #include "net/instaweb/rewriter/public/server_context.h" 00027 #include "net/instaweb/util/public/basictypes.h" 00028 #include "net/instaweb/util/public/property_cache.h" 00029 #include "net/instaweb/util/public/proto_util.h" 00030 #include "net/instaweb/util/public/scoped_ptr.h" 00031 #include "net/instaweb/util/public/string.h" 00032 #include "net/instaweb/util/public/string_util.h" 00033 00034 namespace net_instaweb { 00035 00036 enum PropertyCacheDecodeResult { 00037 kPropertyCacheDecodeNotFound, 00038 kPropertyCacheDecodeExpired, 00039 kPropertyCacheDecodeParseError, 00040 kPropertyCacheDecodeOk 00041 }; 00042 00045 const PropertyValue* DecodeFromPropertyCacheHelper( 00046 const PropertyCache* cache, 00047 AbstractPropertyPage* page, 00048 const PropertyCache::Cohort* cohort, 00049 StringPiece property_name, 00050 int64 cache_ttl_ms, 00051 PropertyCacheDecodeResult* status); 00052 00060 template<typename T> 00061 T* DecodeFromPropertyCache(const PropertyCache* cache, 00062 AbstractPropertyPage* page, 00063 const PropertyCache::Cohort* cohort, 00064 StringPiece property_name, 00065 int64 cache_ttl_ms, 00066 PropertyCacheDecodeResult* status) { 00067 const PropertyValue* property_value = 00068 DecodeFromPropertyCacheHelper(cache, page, cohort, property_name, 00069 cache_ttl_ms, status); 00070 if (property_value == NULL) { 00072 return NULL; 00073 } 00074 00075 scoped_ptr<T> result(new T); 00076 ArrayInputStream input(property_value->value().data(), 00077 property_value->value().size()); 00078 if (!result->ParseFromZeroCopyStream(&input)) { 00079 *status = kPropertyCacheDecodeParseError; 00080 return NULL; 00081 } 00082 00083 *status = kPropertyCacheDecodeOk; 00084 return result.release(); 00085 } 00086 00089 template<typename T> 00090 T* DecodeFromPropertyCache(RewriteDriver* driver, 00091 const PropertyCache::Cohort* cohort, 00092 StringPiece property_name, 00093 int64 cache_ttl_ms, 00094 PropertyCacheDecodeResult* status) { 00095 return DecodeFromPropertyCache<T>( 00096 driver->server_context()->page_property_cache(), 00097 driver->property_page(), 00098 cohort, 00099 property_name, 00100 cache_ttl_ms, 00101 status); 00102 } 00103 00104 enum PropertyCacheUpdateResult { 00105 kPropertyCacheUpdateNotFound, 00106 kPropertyCacheUpdateEncodeError, 00107 kPropertyCacheUpdateOk 00108 }; 00109 00110 PropertyCacheUpdateResult UpdateInPropertyCache( 00111 const protobuf::MessageLite& value, 00112 const PropertyCache::Cohort* cohort, 00113 StringPiece property_name, 00114 bool write_cohort, 00115 AbstractPropertyPage* page); 00116 00121 inline PropertyCacheUpdateResult UpdateInPropertyCache( 00122 const protobuf::MessageLite& value, RewriteDriver* driver, 00123 const PropertyCache::Cohort* cohort, StringPiece property_name, 00124 bool write_cohort) { 00125 AbstractPropertyPage* page = driver->property_page(); 00126 return UpdateInPropertyCache( 00127 value, cohort, property_name, write_cohort, page); 00128 } 00129 00130 } 00131 00132 #endif ///< NET_INSTAWEB_REWRITER_PUBLIC_PROPERTY_CACHE_UTIL_H_