OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Rev

Rev 144 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
17 ilm 1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
5
 *
6
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
7
 * only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
8
 * copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
9
 * language governing permissions and limitations under the License.
10
 *
11
 * When distributing the software, include this License Header Notice in each file.
12
 */
13
 
14
 package org.openconcerto.sql.model;
15
 
16
import java.util.HashMap;
17
import java.util.Map;
18
import java.util.Set;
19
 
20
public class SQLBackgroundTableCache {
21
    private static SQLBackgroundTableCache instance;
174 ilm 22
    private Map<SQLTable, SQLBackgroundTableCacheItem> list = new HashMap<>();
17 ilm 23
 
24
    /**
25
     * @param args
26
     */
27
    public static void main(String[] args) {
28
        SQLBackgroundTableCache cache = SQLBackgroundTableCache.getInstance();
29
        SQLTable t = new SQLTable(null, "test");
30
        cache.add(t, 30);
31
 
32
    }
33
 
67 ilm 34
    private SQLBackgroundTableCache() {
35
 
36
    }
37
 
38
    public static synchronized SQLBackgroundTableCache getInstance() {
39
        if (instance == null) {
40
            instance = new SQLBackgroundTableCache();
41
        }
42
        return instance;
43
    }
44
 
45
    public synchronized void add(SQLTable t, int second) {
46
        if (!isCached(t)) {
47
            final SQLBackgroundTableCacheItem item = new SQLBackgroundTableCacheItem(t, second);
48
            this.list.put(t, item);
49
        }
50
    }
51
 
52
    public void startCacheWatcher() {
17 ilm 53
        Thread t = new Thread(new Runnable() {
54
 
55
            @Override
56
            public void run() {
57
                while (true) {
58
                    try {
59
                        synchronized (SQLBackgroundTableCache.this) {
60
                            Set<SQLTable> set = list.keySet();
61
                            for (SQLTable table : set) {
62
                                getCacheForTable(table);
63
                            }
64
                        }
65
                    } catch (Exception e) {
66
                        e.printStackTrace();
67
                    }
68
                    try {// 1 minute
69
                        Thread.sleep(60 * 1000);
70
                    } catch (InterruptedException e) {
71
                        e.printStackTrace();
72
                    }
73
                }
74
 
75
            }
76
        });
77
        t.setName("SQLBackgroundTableCache");
78
        t.setPriority(Thread.MIN_PRIORITY);
79
        t.setDaemon(true);
80
        t.start();
81
    }
82
 
83
    public synchronized boolean isCached(SQLTable t) {
84
        return this.list.containsKey(t);
85
    }
86
 
87
    public synchronized SQLBackgroundTableCacheItem getCacheForTable(SQLTable t) {
144 ilm 88
        SQLBackgroundTableCacheItem item = this.list.get(t);
17 ilm 89
        if (item != null) {
90
            item.reloadFromDbIfNeeded();
144 ilm 91
        } else {
92
            System.err.println("SQLBackgroundTableCache.getCacheForTable() WARNING " + t.getName() + " is not registered (use add to register)");
174 ilm 93
            // Start cache without autorefresh for Caisse offline
94
            this.add(t, -1);
144 ilm 95
            item = this.list.get(t);
96
            item.reloadFromDbIfNeeded();
17 ilm 97
        }
98
        return item;
99
    }
100
 
101
}