Skip to content

Commit

Permalink
Add simple lru cache into utils. (envoyproxy#8)
Browse files Browse the repository at this point in the history
* Add simple lru cache into utils.

* Format files.
  • Loading branch information
chowchow316 authored Jan 17, 2017
1 parent f585f33 commit 9c38034
Show file tree
Hide file tree
Showing 5 changed files with 2,300 additions and 0 deletions.
24 changes: 24 additions & 0 deletions mixerclient/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@ cc_library(
],
)

cc_library(
name = "simple_lru_cache",
srcs = ["utils/google_macros.h"],
hdrs = [
"utils/simple_lru_cache.h",
"utils/simple_lru_cache_inl.h",
],
visibility = ["//visibility:public"],
)

cc_test(
name = "mixer_client_impl_test",
size = "small",
Expand All @@ -43,3 +53,17 @@ cc_test(
"//external:googletest_main",
],
)

cc_test(
name = "simple_lru_cache_test",
size = "small",
srcs = ["utils/simple_lru_cache_test.cc"],
linkopts = [
"-lm",
"-lpthread",
],
deps = [
":simple_lru_cache",
"//external:googletest_main",
],
)
24 changes: 24 additions & 0 deletions mixerclient/utils/google_macros.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/* Copyright 2017 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef MIXERCLIENT_UTILS_GOOGLE_MACROS_H_
#define MIXERCLIENT_UTILS_GOOGLE_MACROS_H_

#undef GOOGLE_DISALLOW_EVIL_CONSTRUCTORS
#define GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(TypeName) \
TypeName(const TypeName&); \
void operator=(const TypeName&)

#endif // MIXERCLIENT_UTILS_GOOGLE_MACROS_H_
50 changes: 50 additions & 0 deletions mixerclient/utils/simple_lru_cache.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/* Copyright 2017 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

// For inclusion in .h files. The real class definition is in
// simple_lru_cache_inl.h.

#ifndef MIXERCLIENT_UTILS_SIMPLE_LRU_CACHE_H_
#define MIXERCLIENT_UTILS_SIMPLE_LRU_CACHE_H_

#include <functional>
#include <unordered_map> // for hash<>

namespace istio {
namespace mixer_client {

namespace internal {
template <typename T>
struct SimpleLRUHash : public std::hash<T> {};
} // namespace internal

template <typename Key, typename Value,
typename H = internal::SimpleLRUHash<Key>,
typename EQ = std::equal_to<Key> >
class SimpleLRUCache;

// Deleter is a functor that defines how to delete a Value*. That is, it
// contains a public method:
// operator() (Value* value)
// See example in the associated unittest.
template <typename Key, typename Value, typename Deleter,
typename H = internal::SimpleLRUHash<Key>,
typename EQ = std::equal_to<Key> >
class SimpleLRUCacheWithDeleter;

} // namespace mixer_client
} // namespace istio

#endif // MIXERCLIENT_UTILS_SIMPLE_LRU_CACHE_H_
Loading

0 comments on commit 9c38034

Please sign in to comment.