1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// Copyright 2015-2020 Parity Technologies (UK) Ltd.
// This file is part of OpenEthereum.

// OpenEthereum is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// OpenEthereum is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with OpenEthereum.  If not, see <http://www.gnu.org/licenses/>.

use parking_lot::RwLock;
use std::{collections::HashMap, fmt, sync::Arc};

/// Generic cache implementation
#[derive(Debug, Clone)]
pub struct Cache<K, V> {
    values: Arc<RwLock<HashMap<K, V>>>,
    limit: usize,
    name: String,
}

impl<K, V> Cache<K, V> {
    /// Create new named cache with a limit of `limit` entries.
    pub fn new(name: &str, limit: usize) -> Self {
        Self {
            values: Arc::new(RwLock::new(HashMap::with_capacity(limit / 2))),
            limit,
            name: name.to_string(),
        }
    }

    /// Retrieve a cached value for given key.
    pub fn get<Q: ?Sized>(&self, key: &Q) -> Option<V>
    where
        K: std::hash::Hash + std::cmp::Eq + std::borrow::Borrow<Q>,
        Q: std::hash::Hash + std::cmp::Eq,
        V: Clone,
    {
        self.values.read().get(key).cloned()
    }

    /// Inserts a value computed from f into the cache if it is not already cached,
    /// then returns the value.
    pub fn get_or_insert<F>(&self, key: K, f: F) -> V
    where
        K: std::hash::Hash + std::cmp::Eq,
        V: Clone,
        F: FnOnce() -> V,
    {
        if let Some(value) = self.get(&key) {
            // The corresponding value was cached.
            return value;
        }

        // We don't check again if cache has been populated.
        // We assume that it's not THAT expensive to fetch required data from state.
        let mut cache = self.values.write();
        let value = f();
        cache.insert(key, value.clone());

        if cache.len() < self.limit {
            return value;
        }

        debug!(target: "txpool", "{}Cache: reached limit.", self.name().to_string());
        trace_time!("txpool_cache:clear");

        // Remove excessive amount of entries from the cache.
        let remaining: Vec<_> = cache.drain().skip(self.limit / 2).collect();
        for (k, v) in remaining {
            cache.insert(k, v);
        }

        value
    }

    /// Clear all entries from the cache.
    pub fn clear(&self) {
        self.values.write().clear();
    }

    /// Returns the number of elements in the cache.
    pub fn len(&self) -> usize {
        self.values.read().len()
    }

    /// Returns maximum number of elements the cache can keep.
    pub fn limit(&self) -> usize {
        self.limit
    }

    /// Returns the name of the cache
    pub fn name(&self) -> &str {
        &self.name
    }
}

pub(super) struct CachedClient<'a, C: 'a, K, V> {
    client: &'a C,
    cache: &'a Cache<K, V>,
}

impl<'a, C: 'a, K, V> Clone for CachedClient<'a, C, K, V> {
    fn clone(&self) -> Self {
        Self {
            client: self.client,
            cache: self.cache,
        }
    }
}

impl<'a, C: 'a, K, V> fmt::Debug for CachedClient<'a, C, K, V> {
    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        fmt.debug_struct("CachedClient")
            .field("name", &self.cache.name())
            .field("cache", &self.cache.len())
            .field("limit", &self.cache.limit())
            .finish()
    }
}

impl<'a, C: 'a, K, V> CachedClient<'a, C, K, V> {
    pub fn new(client: &'a C, cache: &'a Cache<K, V>) -> Self {
        Self { client, cache }
    }

    pub fn cache(&self) -> &'a Cache<K, V> {
        self.cache
    }

    pub fn client(&self) -> &'a C {
        self.client
    }
}