Page Speed Optimization Libraries
1.4.26.1
|
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 00051 template<typename T> 00052 T* DecodeFromPropertyCache(RewriteDriver* driver, 00053 StringPiece cohort_name, 00054 StringPiece property_name, 00055 int64 cache_ttl_ms, 00056 PropertyCacheDecodeResult* status) { 00057 scoped_ptr<T> result; 00058 const PropertyCache* cache = driver->server_context()->page_property_cache(); 00059 const PropertyCache::Cohort* cohort = cache->GetCohort(cohort_name); 00060 PropertyPage* page = driver->property_page(); 00061 if (cohort == NULL || page == NULL) { 00062 *status = kPropertyCacheDecodeNotFound; 00063 return NULL; 00064 } 00065 00066 PropertyValue* property_value = page->GetProperty(cohort, property_name); 00067 if (property_value == NULL || !property_value->has_value()) { 00068 *status = kPropertyCacheDecodeNotFound; 00069 return NULL; 00070 } 00071 00072 if ((cache_ttl_ms != -1) && cache->IsExpired(property_value, cache_ttl_ms)) { 00073 *status = kPropertyCacheDecodeExpired; 00074 return NULL; 00075 } 00076 00077 result.reset(new T); 00078 ArrayInputStream input(property_value->value().data(), 00079 property_value->value().size()); 00080 if (!result->ParseFromZeroCopyStream(&input)) { 00081 *status = kPropertyCacheDecodeParseError; 00082 return NULL; 00083 } 00084 00085 *status = kPropertyCacheDecodeOk; 00086 return result.release(); 00087 } 00088 00089 enum PropertyCacheUpdateResult { 00090 kPropertyCacheUpdateNotFound, 00091 kPropertyCacheUpdateEncodeError, 00092 kPropertyCacheUpdateOk 00093 }; 00094 00099 template<typename T> 00100 PropertyCacheUpdateResult UpdateInPropertyCache(const T& value, 00101 RewriteDriver* driver, 00102 StringPiece cohort_name, 00103 StringPiece property_name, 00104 bool write_cohort) { 00105 const PropertyCache* cache = driver->server_context()->page_property_cache(); 00106 PropertyPage* page = driver->property_page(); 00107 return UpdateInPropertyCache( 00108 value, cache, cohort_name, property_name, write_cohort, page); 00109 } 00110 00111 template<typename T> 00112 PropertyCacheUpdateResult UpdateInPropertyCache(const T& value, 00113 const PropertyCache* cache, 00114 StringPiece cohort_name, 00115 StringPiece property_name, 00116 bool write_cohort, 00117 PropertyPage* page) { 00118 const PropertyCache::Cohort* cohort = cache->GetCohort(cohort_name); 00119 if (cohort == NULL || page == NULL) { 00120 return kPropertyCacheUpdateNotFound; 00121 } 00122 00123 PropertyValue* property_value = page->GetProperty(cohort, property_name); 00124 if (property_value == NULL) { 00125 return kPropertyCacheUpdateNotFound; 00126 } 00127 00128 GoogleString buf; 00129 if (!value.SerializeToString(&buf)) { 00130 return kPropertyCacheUpdateEncodeError; 00131 } 00132 00133 page->UpdateValue(cohort, property_name, buf); 00134 00135 if (write_cohort) { 00136 page->WriteCohort(cohort); 00137 } 00138 00139 return kPropertyCacheUpdateOk; 00140 } 00141 00142 } 00143 00144 #endif ///< NET_INSTAWEB_REWRITER_PUBLIC_PROPERTY_CACHE_UTIL_H_