]> git.basschouten.com Git - openhab-addons.git/blob
ef39f19ea90b9f8959cf0f19a018a390a5ca06af
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
7  * This program and the accompanying materials are made available under the
8  * terms of the Eclipse Public License 2.0 which is available at
9  * http://www.eclipse.org/legal/epl-2.0
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.shelly.internal.manager;
14
15 import java.util.Date;
16 import java.util.Map;
17 import java.util.concurrent.ConcurrentHashMap;
18
19 import org.eclipse.jdt.annotation.Nullable;
20
21 /**
22  * {@link ShellyManagerCache} implements a cache with expiring times of the entries
23  *
24  * @author Markus Michels - Initial contribution
25  */
26 public class ShellyManagerCache<K, V> extends ConcurrentHashMap<K, V> {
27
28     private static final long serialVersionUID = 1L;
29
30     private Map<K, Long> timeMap = new ConcurrentHashMap<>();
31     private long expiryInMillis = ShellyManagerConstants.CACHE_TIMEOUT_DEF_MIN * 60 * 1000; // Default 1h
32
33     public ShellyManagerCache() {
34         initialize();
35     }
36
37     public ShellyManagerCache(long expiryInMillis) {
38         this.expiryInMillis = expiryInMillis;
39         initialize();
40     }
41
42     void initialize() {
43         new CleanerThread().start();
44     }
45
46     @Override
47     public V put(K key, V value) {
48         Date date = new Date();
49         timeMap.put(key, date.getTime());
50         return super.put(key, value);
51     }
52
53     @Override
54     public void putAll(@Nullable Map<? extends K, ? extends V> m) {
55         if (m == null) {
56             throw new IllegalArgumentException();
57         }
58         for (K key : m.keySet()) {
59             V value = m.get(key);
60             if (value != null) { // don't allow null values
61                 put(key, value);
62             }
63         }
64     }
65
66     @Override
67     public V putIfAbsent(K key, V value) {
68         if (!containsKey(key)) {
69             return put(key, value);
70         } else {
71             return get(key);
72         }
73     }
74
75     class CleanerThread extends Thread {
76         @Override
77         public void run() {
78             while (true) {
79                 cleanMap();
80                 try {
81                     Thread.sleep(expiryInMillis / 2);
82                 } catch (InterruptedException e) {
83                 }
84             }
85         }
86
87         private void cleanMap() {
88             long currentTime = new Date().getTime();
89             for (K key : timeMap.keySet()) {
90                 if (currentTime > (timeMap.get(key) + expiryInMillis)) {
91                     remove(key);
92                     timeMap.remove(key);
93                 }
94             }
95         }
96     }
97 }