source: Ballon/web/WEB-INF/lib/mysql-connector-java-5.1.21/src/com/mysql/jdbc/PerConnectionLRUFactory.java @ 766

Last change on this file since 766 was 766, checked in by npipsl, 11 years ago
File size: 2.3 KB
Line 
1/*
2  Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
3
4  The MySQL Connector/J is licensed under the terms of the GPLv2
5  <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>, like most MySQL Connectors.
6  There are special exceptions to the terms and conditions of the GPLv2 as it is applied to
7  this software, see the FLOSS License Exception
8  <http://www.mysql.com/about/legal/licensing/foss-exception.html>.
9
10  This program is free software; you can redistribute it and/or modify it under the terms
11  of the GNU General Public License as published by the Free Software Foundation; version 2
12  of the License.
13
14  This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
15  without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16  See the GNU General Public License for more details.
17
18  You should have received a copy of the GNU General Public License along with this
19  program; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth
20  Floor, Boston, MA 02110-1301  USA
21 
22 */
23
24package com.mysql.jdbc;
25
26import java.sql.SQLException;
27import java.util.Properties;
28
29import com.mysql.jdbc.PreparedStatement.ParseInfo;
30import com.mysql.jdbc.util.LRUCache;
31
32public class PerConnectionLRUFactory implements CacheAdapterFactory<String, ParseInfo> {
33
34        public CacheAdapter<String, ParseInfo> getInstance(Connection forConnection,
35                        String url, int cacheMaxSize, int maxKeySize,
36                        Properties connectionProperties) throws SQLException {
37
38                return new PerConnectionLRU(forConnection, cacheMaxSize, maxKeySize);
39        }
40
41        class PerConnectionLRU implements CacheAdapter<String, ParseInfo> {
42                private final int cacheSqlLimit;
43                private final LRUCache cache;
44                private final Connection conn;
45               
46                protected PerConnectionLRU(Connection forConnection, int cacheMaxSize,
47                                int maxKeySize) {
48                        final int cacheSize = cacheMaxSize;
49                        cacheSqlLimit = maxKeySize;
50                        cache = new LRUCache(cacheSize);
51                        conn = forConnection;
52                }
53               
54                public ParseInfo get(String key) {
55                        if (key == null || key.length() > cacheSqlLimit) {
56                                return null;
57                        }
58
59                        synchronized (conn) {
60                                return (ParseInfo) cache.get(key);
61                        }
62                }
63
64                public void put(String key, ParseInfo value) {
65                        if (key == null || key.length() > cacheSqlLimit) {
66                                return;
67                        }
68
69                        synchronized (conn) {
70                                cache.put(key, value);
71                        }
72                }
73        }
74}
Note: See TracBrowser for help on using the repository browser.