4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package net.oschina.j2cache;
17 |
18 | import java.util.Collection;
19 | import java.util.Map;
20 |
21 | /**
22 | * Cache Data Operation Interface
23 | *
24 | * @author Winter Lau(javayou@gmail.com)
25 | */
26 | public interface Cache {
27 |
28 | /**
29 | * Get an item from the cache, nontransactionally
30 | *
31 | * @param key cache key
32 | * @return the cached object or null
33 | */
34 | Object get(String key) ;
35 |
36 | /**
37 | * 批量获取缓存对象
38 | * @param keys cache keys
39 | * @return return key-value objects
40 | */
41 | Map get(Collection keys);
42 |
43 | /**
44 | * 判断缓存是否存在
45 | * @param key cache key
46 | * @return true if key exists
47 | */
48 | default boolean exists(String key) {
49 | return get(key) != null;
50 | }
51 |
52 | /**
53 | * Add an item to the cache, nontransactionally, with
54 | * failfast semantics
55 | *
56 | * @param key cache key
57 | * @param value cache value
58 | */
59 | void put(String key, Object value);
60 |
61 | /**
62 | * 批量插入数据
63 | * @param elements objects to be put in cache
64 | */
65 | void put(Map elements);
66 |
67 | /**
68 | * Return all keys
69 | *
70 | * @return 返回键的集合
71 | */
72 | Collection keys() ;
73 |
74 | /**
75 | * Remove items from the cache
76 | *
77 | * @param keys Cache key
78 | */
79 | void evict(String...keys);
80 |
81 | /**
82 | * Clear the cache
83 | */
84 | void clear();
85 |
86 | }
87 |
--------------------------------------------------------------------------------
/core/src/net/oschina/j2cache/CacheException.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2015-2017, Winter Lau (javayou@gmail.com).
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package net.oschina.j2cache;
17 |
18 | /**
19 | * J2Cache exception
20 | *
21 | * @author Winter Lau (javayou@gmail.com)
22 | */
23 | public class CacheException extends RuntimeException {
24 |
25 | public CacheException(String s) {
26 | super(s);
27 | }
28 |
29 | public CacheException(String s, Throwable e) {
30 | super(s, e);
31 | }
32 |
33 | public CacheException(Throwable e) {
34 | super(e);
35 | }
36 |
37 | }
38 |
--------------------------------------------------------------------------------
/core/src/net/oschina/j2cache/CacheExpiredListener.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2015-2017, Winter Lau (javayou@gmail.com).
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package net.oschina.j2cache;
17 |
18 | /**
19 | * When cached data expired in ehcache, this listener will be invoked.
20 | *
21 | * @author Winter Lau(javayou@gmail.com)
22 | */
23 | public interface CacheExpiredListener {
24 |
25 | /**
26 | * 缓存因为超时失效后触发的通知
27 | * @param region 缓存 region
28 | * @param key 缓存 key
29 | */
30 | void notifyElementExpired(String region, String key) ;
31 |
32 | }
33 |
--------------------------------------------------------------------------------
/core/src/net/oschina/j2cache/CacheProvider.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2015-2017, Winter Lau (javayou@gmail.com).
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package net.oschina.j2cache;
17 |
18 | import java.util.Collection;
19 | import java.util.Properties;
20 |
21 | /**
22 | * Support for pluggable caches.
23 | * @author Winter Lau(javayou@gmail.com)
24 | */
25 | public interface CacheProvider {
26 |
27 | /**
28 | * 缓存的标识名称
29 | * @return return cache provider name
30 | */
31 | String name();
32 |
33 | /**
34 | * 缓存的层级
35 | * @return current provider level
36 | */
37 | int level();
38 |
39 | default boolean isLevel(int level) {
40 | return (level() & level) == level;
41 | }
42 |
43 | /**
44 | * Configure the cache
45 | *
46 | * @param regionName the name of the cache region
47 | * @param listener listener for expired elements
48 | * @return return cache instance
49 | */
50 | Cache buildCache(String regionName, CacheExpiredListener listener);
51 |
52 | /**
53 | * Configure the cache with timeToLiveInMills
54 | * @param region cache region name
55 | * @param timeToLiveInSeconds time to live in second
56 | * @param listener listener for expired elements
57 | * @return return cache instance
58 | */
59 | Cache buildCache(String region, long timeToLiveInSeconds, CacheExpiredListener listener);
60 |
61 | /**
62 | * Remove a cache region
63 | * @param region cache region name
64 | */
65 | default void removeCache(String region) {}
66 |
67 | /**
68 | * Return all channels defined in first level cache
69 | * @return all regions name
70 | */
71 | Collection regions();
72 |
73 | /**
74 | * Callback to perform any necessary initialization of the underlying cache implementation
75 | * during SessionFactory construction.
76 | *
77 | * @param props current configuration settings.
78 | */
79 | void start(Properties props);
80 |
81 | /**
82 | * Callback to perform any necessary cleanup of the underlying cache implementation
83 | * during SessionFactory.close().
84 | */
85 | void stop();
86 |
87 | }
88 |
--------------------------------------------------------------------------------
/core/src/net/oschina/j2cache/J2Cache.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2015-2017, Winter Lau (javayou@gmail.com).
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package net.oschina.j2cache;
17 |
18 | import java.io.IOException;
19 |
20 | /**
21 | * J2Cache 的缓存入口
22 | * @author Winter Lau(javayou@gmail.com)
23 | */
24 | public class J2Cache {
25 |
26 | private final static String CONFIG_FILE = "/j2cache.properties";
27 |
28 | private final static J2CacheBuilder builder;
29 |
30 | static {
31 | try {
32 | J2CacheConfig config = J2CacheConfig.initFromConfig(CONFIG_FILE);
33 | builder = J2CacheBuilder.init(config);
34 | } catch (IOException e) {
35 | throw new CacheException("Failed to load j2cache configuration " + CONFIG_FILE, e);
36 | }
37 | }
38 |
39 | /**
40 | * 返回缓存操作接口
41 | * @return CacheChannel
42 | */
43 | public static CacheChannel getChannel(){
44 | return builder.getChannel();
45 | }
46 |
47 | /**
48 | * 关闭 J2Cache
49 | */
50 | public static void close() {
51 | builder.close();
52 | }
53 | }
54 |
--------------------------------------------------------------------------------
/core/src/net/oschina/j2cache/Level1Cache.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2015-2017, Winter Lau (javayou@gmail.com).
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package net.oschina.j2cache;
17 |
18 | /**
19 | * 一级缓存接口
20 | * @author Winter Lau(javayou@gmail.com)
21 | */
22 | public interface Level1Cache extends Cache {
23 |
24 | /**
25 | * 返回该缓存区域的 TTL 设置(单位:秒)
26 | * @return true if cache support ttl setting
27 | */
28 | long ttl();
29 |
30 | /**
31 | * 返回该缓存区域中,内存存储对象的最大数量
32 | * @return cache size in memory
33 | */
34 | long size();
35 |
36 | }
37 |
--------------------------------------------------------------------------------
/core/src/net/oschina/j2cache/NullCache.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2015-2017, Winter Lau (javayou@gmail.com).
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package net.oschina.j2cache;
17 |
18 | import java.util.Collection;
19 | import java.util.Collections;
20 | import java.util.List;
21 | import java.util.Map;
22 |
23 | /**
24 | * 空的缓存Provider
25 | * @author Winter Lau(javayou@gmail.com)
26 | */
27 | public class NullCache implements Level1Cache, Level2Cache {
28 |
29 | @Override
30 | public long ttl() {
31 | return -1;
32 | }
33 |
34 | @Override
35 | public long size() {
36 | return 0;
37 | }
38 |
39 | @Override
40 | public Object get(String key) {
41 | return null;
42 | }
43 |
44 | @Override
45 | public void put(String key, Object value) {
46 | }
47 |
48 | @Override
49 | public Collection keys() {
50 | return Collections.emptyList();
51 | }
52 |
53 | @Override
54 | public Map get(Collection keys) {
55 | return Collections.emptyMap();
56 | }
57 |
58 | @Override
59 | public boolean exists(String key) {
60 | return false;
61 | }
62 |
63 | @Override
64 | public void put(Map elements) {
65 | }
66 |
67 | @Override
68 | public byte[] getBytes(String key) {
69 | return null;
70 | }
71 |
72 | @Override
73 | public List getBytes(Collection key) {
74 | return Collections.emptyList();
75 | }
76 |
77 | @Override
78 | public void setBytes(String key, byte[] bytes) {
79 | }
80 |
81 | @Override
82 | public void setBytes(Map bytes) {
83 | }
84 |
85 | @Override
86 | public void evict(String...keys) {
87 | }
88 |
89 | @Override
90 | public void clear() {
91 |
92 | }
93 | }
94 |
--------------------------------------------------------------------------------
/core/src/net/oschina/j2cache/NullCacheProvider.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2015-2017, Winter Lau (javayou@gmail.com).
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package net.oschina.j2cache;
17 |
18 | import java.util.Collection;
19 | import java.util.Properties;
20 |
21 | /**
22 | * @author Winter Lau(javayou@gmail.com)
23 | */
24 | public class NullCacheProvider implements CacheProvider {
25 |
26 | private final static NullCache cache = new NullCache();
27 |
28 | @Override
29 | public String name() {
30 | return "none";
31 | }
32 |
33 | @Override
34 | public int level() {
35 | return CacheObject.LEVEL_1 | CacheObject.LEVEL_2;
36 | }
37 |
38 | @Override
39 | public Cache buildCache(String regionName, CacheExpiredListener listener) throws CacheException {
40 | return cache;
41 | }
42 |
43 | @Override
44 | public Cache buildCache(String region, long timeToLiveInSeconds, CacheExpiredListener listener) {
45 | return cache;
46 | }
47 |
48 | @Override
49 | public void start(Properties props) throws CacheException {
50 | }
51 |
52 | @Override
53 | public Collection regions() {
54 | return null;
55 | }
56 |
57 | @Override
58 | public void stop() {
59 | }
60 |
61 | }
62 |
--------------------------------------------------------------------------------
/core/src/net/oschina/j2cache/NullObject.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2015-2017, Winter Lau (javayou@gmail.com).
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package net.oschina.j2cache;
17 |
18 | import java.io.Serializable;
19 |
20 | /**
21 | * Null Object
22 | * @author Winter Lau(javayou@gmail.com)
23 | */
24 | public class NullObject implements Serializable {
25 | }
26 |
--------------------------------------------------------------------------------
/core/src/net/oschina/j2cache/caffeine/CaffeineCache.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2015-2017, Winter Lau (javayou@gmail.com).
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package net.oschina.j2cache.caffeine;
17 |
18 | import com.github.benmanes.caffeine.cache.Cache;
19 | import net.oschina.j2cache.Level1Cache;
20 |
21 | import java.util.Arrays;
22 | import java.util.Collection;
23 | import java.util.Map;
24 |
25 | /**
26 | * Caffeine cache
27 | *
28 | * @author Winter Lau(javayou@gmail.com)
29 | */
30 | public class CaffeineCache implements Level1Cache {
31 |
32 | private Cache cache;
33 | private long size ;
34 | private long expire ;
35 |
36 | public CaffeineCache(Cache cache, long size, long expire) {
37 | this.cache = cache;
38 | this.size = size;
39 | this.expire = expire;
40 | }
41 |
42 | @Override
43 | public long ttl() {
44 | return expire;
45 | }
46 |
47 | @Override
48 | public long size() { return size; }
49 |
50 | @Override
51 | public Object get(String key) {
52 | return cache.getIfPresent(key);
53 | }
54 |
55 | @Override
56 | public Map get(Collection keys) {
57 | return cache.getAllPresent(keys);
58 | }
59 |
60 | @Override
61 | public void put(String key, Object value) {
62 | cache.put(key, value);
63 | }
64 |
65 | @Override
66 | public void put(Map elements) {
67 | cache.putAll(elements);
68 | }
69 |
70 | @Override
71 | public Collection keys() {
72 | return cache.asMap().keySet();
73 | }
74 |
75 | @Override
76 | public void evict(String...keys) {
77 | cache.invalidateAll(Arrays.asList(keys));
78 | }
79 |
80 | @Override
81 | public void clear() {
82 | cache.invalidateAll();
83 | }
84 | }
85 |
--------------------------------------------------------------------------------
/core/src/net/oschina/j2cache/cluster/NoneClusterPolicy.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2015-2017, Winter Lau (javayou@gmail.com).
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package net.oschina.j2cache.cluster;
17 |
18 | import net.oschina.j2cache.CacheProviderHolder;
19 | import net.oschina.j2cache.Command;
20 |
21 | import java.util.Properties;
22 |
23 | /**
24 | * 实现空的集群通知策略
25 | * @author Winter Lau(javayou@gmail.com)
26 | */
27 | public class NoneClusterPolicy implements ClusterPolicy {
28 |
29 | private int LOCAL_COMMAND_ID = Command.genRandomSrc(); //命令源标识,随机生成,每个节点都有唯一标识
30 |
31 | @Override
32 | public boolean isLocalCommand(Command cmd) {
33 | return cmd.getSrc() == LOCAL_COMMAND_ID;
34 | }
35 |
36 | @Override
37 | public void connect(Properties props, CacheProviderHolder holder) {
38 | }
39 |
40 | @Override
41 | public void disconnect() {
42 | }
43 |
44 | @Override
45 | public void publish(Command cmd) {
46 | }
47 |
48 | @Override
49 | public void evict(String region, String... keys) {
50 | }
51 |
52 | @Override
53 | public void clear(String region) {
54 | }
55 | }
56 |
--------------------------------------------------------------------------------
/core/src/net/oschina/j2cache/lettuce/LettuceByteCodec.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2015-2017, Winter Lau (javayou@gmail.com).
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package net.oschina.j2cache.lettuce;
17 |
18 | import io.lettuce.core.codec.RedisCodec;
19 |
20 | import java.nio.ByteBuffer;
21 |
22 | /**
23 | * 使用字节编码
24 | * @author Winter Lau (javayou@gmail.com)
25 | */
26 | public class LettuceByteCodec implements RedisCodec {
27 |
28 | private static final byte[] EMPTY = new byte[0];
29 |
30 | @Override
31 | public String decodeKey(ByteBuffer byteBuffer) {
32 | return new String(getBytes(byteBuffer));
33 | }
34 |
35 | @Override
36 | public byte[] decodeValue(ByteBuffer byteBuffer) {
37 | return getBytes(byteBuffer);
38 | }
39 |
40 | @Override
41 | public ByteBuffer encodeKey(String s) {
42 | return ByteBuffer.wrap(s.getBytes());
43 | }
44 |
45 | @Override
46 | public ByteBuffer encodeValue(byte[] bytes) {
47 | return ByteBuffer.wrap(bytes);
48 | }
49 |
50 |
51 | private static byte[] getBytes(ByteBuffer buffer) {
52 | int remaining = buffer.remaining();
53 |
54 | if (remaining == 0) {
55 | return EMPTY;
56 | }
57 |
58 | byte[] b = new byte[remaining];
59 | buffer.get(b);
60 | return b;
61 | }
62 | }
63 |
--------------------------------------------------------------------------------
/core/src/net/oschina/j2cache/lettuce/LettuceCache.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2015-2017, Winter Lau (javayou@gmail.com).
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package net.oschina.j2cache.util;
17 |
18 | /**
19 | * 反序列化的对象兼容异常
20 | * @author Winter Lau(javayou@gmail.com)
21 | */
22 | public class DeserializeException extends RuntimeException {
23 |
24 | public DeserializeException(String message) {
25 | super(message);
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/core/src/net/oschina/j2cache/util/FSTSerializer.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2015-2017, Winter Lau (javayou@gmail.com).
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package net.oschina.j2cache.util;
17 |
18 | import org.nustaq.serialization.FSTConfiguration;
19 |
20 | /**
21 | * 使用 FST 实现序列化
22 | *
23 | * @author Winter Lau(javayou@gmail.com)
24 | */
25 | public class FSTSerializer implements Serializer {
26 |
27 | private FSTConfiguration fstConfiguration ;
28 |
29 | public FSTSerializer() {
30 | fstConfiguration = FSTConfiguration.getDefaultConfiguration();
31 | fstConfiguration.setClassLoader(Thread.currentThread().getContextClassLoader());
32 | }
33 |
34 | @Override
35 | public String name() {
36 | return "fst";
37 | }
38 |
39 | @Override
40 | public byte[] serialize(Object obj) {
41 | return fstConfiguration.asByteArray(obj);
42 | }
43 |
44 | @Override
45 | public Object deserialize(byte[] bytes) {
46 | return fstConfiguration.asObject(bytes);
47 | }
48 |
49 | }
50 |
--------------------------------------------------------------------------------
/core/src/net/oschina/j2cache/util/FastjsonSerializer.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2015-2017, Winter Lau (javayou@gmail.com).
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package net.oschina.j2cache.util;
17 |
18 | import com.alibaba.fastjson.JSON;
19 | import com.alibaba.fastjson.parser.Feature;
20 | import com.alibaba.fastjson.serializer.SerializerFeature;
21 |
22 | /**
23 | * 使用 fastjson 进行对象的 JSON 格式序列化
24 | *
25 | * @author Winter Lau(javayou@gmail.com)
26 | */
27 | public class FastjsonSerializer implements Serializer {
28 |
29 | @Override
30 | public String name() {
31 | return "fastjson";
32 | }
33 |
34 | @Override
35 | public byte[] serialize(Object obj) {
36 | return JSON.toJSONString(obj, SerializerFeature.WriteClassName).getBytes();
37 | }
38 |
39 | @Override
40 | public Object deserialize(byte[] bytes) {
41 | return JSON.parse(new String(bytes), Feature.SupportAutoType);
42 | }
43 |
44 | }
45 |
--------------------------------------------------------------------------------
/core/src/net/oschina/j2cache/util/FseSerializer.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2015-2020, whcrow (v@whcrow.com).
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package net.oschina.j2cache.util;
17 |
18 | import com.jfirer.fse.ByteArray;
19 | import com.jfirer.fse.Fse;
20 |
21 | /**
22 | * 使用 fse 实现序列化
23 | *
24 | * @author whcrow (v@whcrow.com)
25 | */
26 | public class FseSerializer implements Serializer {
27 |
28 | @Override
29 | public String name() {
30 | return "fse";
31 | }
32 |
33 | @Override
34 | public byte[] serialize(Object obj) {
35 | ByteArray buf = ByteArray.allocate(100);
36 | new Fse().serialize(obj, buf);
37 | byte[] resultBytes = buf.toArray();
38 | buf.clear();
39 | return resultBytes;
40 | }
41 |
42 | @Override
43 | public Object deserialize(byte[] bytes) {
44 | if (bytes == null || bytes.length == 0) {
45 | return null;
46 | }
47 | ByteArray buf = ByteArray.allocate(100);
48 | buf.put(bytes);
49 | Object result = new Fse().deSerialize(buf);
50 | buf.clear();
51 | return result;
52 | }
53 |
54 | }
55 |
--------------------------------------------------------------------------------
/core/src/net/oschina/j2cache/util/FstSnappySerializer.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2015-2017
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package net.oschina.j2cache.util;
17 |
18 | import java.io.IOException;
19 |
20 | import org.xerial.snappy.Snappy;
21 |
22 | /**
23 | * 在 @FSTSerializer 的基础上增加 snappy ,也就是压缩
24 | */
25 | public class FstSnappySerializer implements Serializer {
26 |
27 | private final Serializer inner;
28 |
29 | public FstSnappySerializer() {
30 | this(new FSTSerializer());
31 | }
32 |
33 | public FstSnappySerializer(Serializer innerSerializer) {
34 | this.inner = innerSerializer;
35 | }
36 |
37 |
38 | @Override
39 | public String name() {
40 | return "fst-snappy";
41 | }
42 |
43 | @Override
44 | public byte[] serialize(Object obj) throws IOException {
45 | return Snappy.compress(inner.serialize(obj));
46 | }
47 |
48 | @Override
49 | public Object deserialize(byte[] bytes) throws IOException {
50 | if (bytes == null || bytes.length == 0)
51 | return null;
52 | return inner.deserialize(Snappy.uncompress(bytes));
53 | }
54 | }
55 |
--------------------------------------------------------------------------------
/core/src/net/oschina/j2cache/util/JavaSerializer.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2015-2017, Winter Lau (javayou@gmail.com).
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package net.oschina.j2cache.util;
17 |
18 | import net.oschina.j2cache.CacheException;
19 |
20 | import java.io.*;
21 |
22 | /**
23 | * 标准的 Java 序列化
24 | *
25 | * @author Winter Lau(javayou@gmail.com)
26 | */
27 | public class JavaSerializer implements Serializer {
28 |
29 | @Override
30 | public String name() {
31 | return "java";
32 | }
33 |
34 | @Override
35 | public byte[] serialize(Object obj) throws IOException {
36 | ByteArrayOutputStream baos = new ByteArrayOutputStream();
37 | try (ObjectOutputStream oos = new ObjectOutputStream(baos)){
38 | oos.writeObject(obj);
39 | return baos.toByteArray();
40 | }
41 | }
42 |
43 | @Override
44 | public Object deserialize(byte[] bits) throws IOException {
45 | if(bits == null || bits.length == 0)
46 | return null;
47 | ByteArrayInputStream bais = new ByteArrayInputStream(bits);
48 | try (ObjectInputStream ois = new ObjectInputStream(bais)){
49 | return ois.readObject();
50 | } catch (ClassNotFoundException e) {
51 | throw new CacheException(e);
52 | }
53 | }
54 |
55 | }
56 |
--------------------------------------------------------------------------------
/core/src/net/oschina/j2cache/util/KryoSerializer.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2015-2017, Winter Lau (javayou@gmail.com).
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package net.oschina.j2cache.util;
17 |
18 | import com.esotericsoftware.kryo.Kryo;
19 | import com.esotericsoftware.kryo.io.Input;
20 | import com.esotericsoftware.kryo.io.Output;
21 |
22 | import java.io.ByteArrayInputStream;
23 | import java.io.ByteArrayOutputStream;
24 | import java.io.IOException;
25 |
26 | /**
27 | * 使用 Kryo 实现序列化
28 | *
29 | * @author Winter Lau(javayou@gmail.com)
30 | */
31 | public class KryoSerializer implements Serializer {
32 |
33 | @Override
34 | public String name() {
35 | return "kryo";
36 | }
37 |
38 | @Override
39 | public byte[] serialize(Object obj) {
40 | ByteArrayOutputStream baos = new ByteArrayOutputStream();
41 | try (Output output = new Output(baos);){
42 | new Kryo().writeClassAndObject(output, obj);
43 | output.flush();
44 | return baos.toByteArray();
45 | }
46 | }
47 |
48 | @Override
49 | public Object deserialize(byte[] bits) {
50 | if(bits == null || bits.length == 0)
51 | return null;
52 | try (Input ois = new Input(new ByteArrayInputStream(bits))){
53 | return new Kryo().readClassAndObject(ois);
54 | }
55 | }
56 |
57 | }
58 |
--------------------------------------------------------------------------------
/core/src/net/oschina/j2cache/util/Serializer.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2015-2017, Winter Lau (javayou@gmail.com).
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package net.oschina.j2cache.util;
17 |
18 | import java.io.IOException;
19 |
20 | /**
21 | * 对象序列化接口
22 | *
23 | * @author Winter Lau(javayou@gmail.com)
24 | */
25 | public interface Serializer {
26 |
27 | /**
28 | * 序列化器的名称,该方法仅用于打印日志的时候显示
29 | * @return 返回序列化器名称
30 | */
31 | String name();
32 |
33 | /**
34 | * 对象序列化到字节数组
35 | * @param obj 待序列化的对象
36 | * @return 返回序列化数据
37 | * @throws IOException io exception
38 | */
39 | byte[] serialize(Object obj) throws IOException ;
40 |
41 | /**
42 | * 反序列化到对象
43 | * @param bytes 反序列化的数据
44 | * @return 返回序列化对象
45 | * @throws IOException io exception
46 | */
47 | Object deserialize(byte[] bytes) throws IOException ;
48 |
49 | }
50 |
--------------------------------------------------------------------------------
/core/test/net/oschina/j2cache/ExpiredTester.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2015-2017, Winter Lau (javayou@gmail.com).
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package net.oschina.j2cache.hibernate3;
17 |
18 | import net.oschina.j2cache.J2Cache;
19 |
20 | import org.hibernate.cache.Cache;
21 | import org.hibernate.cache.CacheException;
22 | import org.hibernate.cache.CacheProvider;
23 | import org.hibernate.cache.Timestamper;
24 |
25 | import java.util.Properties;
26 |
27 | /**
28 | * 为 Hibernate 3 提供 J2Cache 的适配
29 | * @author liao
30 | */
31 | @SuppressWarnings("deprecation")
32 | public class J2CacheProvider implements CacheProvider {
33 |
34 | @Override
35 | public Cache buildCache(String regionName, Properties properties)
36 | throws CacheException {
37 | return new J2HibernateCache(regionName, J2Cache.getChannel());
38 | }
39 |
40 | @Override
41 | public boolean isMinimalPutsEnabledByDefault() {
42 | return true;
43 | }
44 |
45 | @Override
46 | public long nextTimestamp() {
47 | return Timestamper.next();
48 | }
49 |
50 | @Override
51 | public void start(Properties properties) throws CacheException {
52 |
53 | }
54 |
55 | @Override
56 | public void stop() {
57 | J2Cache.getChannel().close();
58 | }
59 |
60 | }
61 |
--------------------------------------------------------------------------------
/modules/hibernate3/src/net/oschina/j2cache/hibernate3/J2CacheRegionFactory.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2015-2017, Winter Lau (javayou@gmail.com).
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package net.oschina.j2cache.hibernate4;
17 |
18 | import net.oschina.j2cache.CacheObject;
19 |
20 | public interface CacheRegion {
21 |
22 | String getName();
23 |
24 | void clear();
25 |
26 | CacheObject get(Object key);
27 |
28 | void put(Object key, Object value);
29 |
30 | void evict(Object key);
31 |
32 | Iterable extends Object> keys();
33 |
34 | }
35 |
--------------------------------------------------------------------------------
/modules/hibernate4/src/net/oschina/j2cache/hibernate4/J2CacheCacheRegion.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2015-2017.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package net.oschina.j2cache.hibernate4;
17 |
18 | import net.oschina.j2cache.CacheChannel;
19 | import net.oschina.j2cache.CacheObject;
20 |
21 | public class J2CacheCacheRegion implements CacheRegion {
22 |
23 | private CacheChannel cacheChannel;
24 | private String region;
25 |
26 | public J2CacheCacheRegion(CacheChannel channel, String region) {
27 | this.cacheChannel = channel;
28 | this.region = region;
29 | }
30 |
31 | @Override
32 | public String getName() {
33 | return this.region;
34 | }
35 |
36 | @Override
37 | public void clear() {
38 | this.cacheChannel.clear(this.region);
39 | }
40 |
41 | @Override
42 | public CacheObject get(Object key) {
43 | return this.cacheChannel.get(this.region, key.toString());
44 | }
45 |
46 | @Override
47 | public void put(Object key, Object value) {
48 | this.cacheChannel.set(this.region, key.toString(), value);
49 | }
50 |
51 | @Override
52 | public void evict(Object key) {
53 | this.cacheChannel.evict(this.region, key.toString());
54 | }
55 |
56 | public Iterable extends Object> keys() {
57 | return this.cacheChannel.keys(this.region);
58 | }
59 | }
60 |
--------------------------------------------------------------------------------
/modules/hibernate4/src/net/oschina/j2cache/hibernate4/J2CacheRegionFactory.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2015-2017.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package net.oschina.j2cache.hibernate4;
17 |
18 | import net.oschina.j2cache.J2Cache;
19 |
20 | import org.hibernate.cache.CacheException;
21 | import org.hibernate.cfg.Settings;
22 |
23 | import java.util.Properties;
24 |
25 |
26 | public class J2CacheRegionFactory extends AbstractJ2CacheRegionFactory {
27 |
28 | private static final String SPRING_CACHEMANAGER = "hibernate.cache.spring.cache_manager";
29 |
30 | private static final String DEFAULT_SPRING_CACHEMANAGER = "cacheManager";
31 |
32 | @SuppressWarnings("UnusedDeclaration")
33 | public J2CacheRegionFactory() {
34 | }
35 |
36 | @SuppressWarnings("UnusedDeclaration")
37 | public J2CacheRegionFactory(Properties prop) {
38 | super();
39 | }
40 |
41 | @Override
42 | public void start(Settings settings, Properties properties) throws CacheException {
43 | this.settings = settings;
44 | if (this.channel == null) {
45 | this.channel = J2Cache.getChannel();
46 | }
47 | }
48 |
49 | @Override
50 | public void stop() {
51 | if (channel != null) {
52 | channel.close();
53 | }
54 | }
55 |
56 | }
--------------------------------------------------------------------------------
/modules/hibernate4/src/net/oschina/j2cache/hibernate4/nonstop/HibernateNonstopCacheExceptionHandler.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2015-2017.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package net.oschina.j2cache.hibernate4.nonstop;
17 |
18 | import net.oschina.j2cache.hibernate4.log.J2CacheMessageLogger;
19 | import org.jboss.logging.Logger;
20 |
21 | public final class HibernateNonstopCacheExceptionHandler {
22 | public static final String HIBERNATE_THROW_EXCEPTION_ON_TIMEOUT_PROPERTY = "J2Cache.hibernate.propagateNonStopCacheException";
23 | public static final String HIBERNATE_LOG_EXCEPTION_STACK_TRACE_PROPERTY = "J2Cache.hibernate.logNonStopCacheExceptionStackTrace";
24 |
25 | private static final J2CacheMessageLogger LOG = Logger.getMessageLogger(J2CacheMessageLogger.class, HibernateNonstopCacheExceptionHandler.class.getName());
26 | private static final HibernateNonstopCacheExceptionHandler INSTANCE = new HibernateNonstopCacheExceptionHandler();
27 |
28 | private HibernateNonstopCacheExceptionHandler() {
29 | }
30 |
31 | public static HibernateNonstopCacheExceptionHandler getInstance() {
32 | return INSTANCE;
33 | }
34 |
35 | public void handleNonstopCacheException(NonStopCacheException nonStopCacheException) {
36 | if (Boolean.getBoolean(HIBERNATE_THROW_EXCEPTION_ON_TIMEOUT_PROPERTY)) {
37 | throw nonStopCacheException;
38 | } else {
39 | if (Boolean.getBoolean(HIBERNATE_LOG_EXCEPTION_STACK_TRACE_PROPERTY)) {
40 | LOG.debug("Ignoring NonstopCacheException - " + nonStopCacheException.getMessage(), nonStopCacheException);
41 | } else {
42 | LOG.debug("Ignoring NonstopCacheException - " + nonStopCacheException.getMessage());
43 | }
44 | }
45 | }
46 | }
--------------------------------------------------------------------------------
/modules/hibernate4/src/net/oschina/j2cache/hibernate4/nonstop/NonStopCacheException.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2015-2017.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package net.oschina.j2cache.hibernate4.nonstop;
17 |
18 | import org.hibernate.cache.CacheException;
19 |
20 | public class NonStopCacheException extends CacheException {
21 |
22 | public NonStopCacheException(final String message, final Throwable cause) {
23 | super(message, cause);
24 | }
25 |
26 | public NonStopCacheException(final String message) {
27 | super(message);
28 | }
29 |
30 | public NonStopCacheException(final Throwable cause) {
31 | super(cause);
32 | }
33 |
34 | }
--------------------------------------------------------------------------------
/modules/hibernate4/src/net/oschina/j2cache/hibernate4/regions/J2CacheCollectionRegion.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2015-2017.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package net.oschina.j2cache.hibernate4.regions;
17 |
18 | import net.oschina.j2cache.hibernate4.CacheRegion;
19 | import net.oschina.j2cache.hibernate4.strategy.J2CacheAccessStrategyFactory;
20 | import org.hibernate.cache.CacheException;
21 | import org.hibernate.cache.spi.CacheDataDescription;
22 | import org.hibernate.cache.spi.CollectionRegion;
23 | import org.hibernate.cache.spi.access.AccessType;
24 | import org.hibernate.cache.spi.access.CollectionRegionAccessStrategy;
25 | import org.hibernate.cfg.Settings;
26 |
27 | import java.util.Properties;
28 |
29 | public class J2CacheCollectionRegion extends J2CacheTransactionalDataRegion implements CollectionRegion {
30 |
31 | public J2CacheCollectionRegion(J2CacheAccessStrategyFactory accessStrategyFactory, CacheRegion underlyingCache, Settings settings, CacheDataDescription metadata, Properties properties) {
32 | super(accessStrategyFactory, underlyingCache, settings, metadata, properties);
33 | }
34 |
35 | @Override
36 | public CollectionRegionAccessStrategy buildAccessStrategy(AccessType accessType) throws CacheException {
37 | return getAccessStrategyFactory().createCollectionRegionAccessStrategy(this, accessType);
38 | }
39 |
40 | }
41 |
--------------------------------------------------------------------------------
/modules/hibernate4/src/net/oschina/j2cache/hibernate4/regions/J2CacheEntityRegion.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2015-2017.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package net.oschina.j2cache.hibernate4.regions;
17 |
18 | import net.oschina.j2cache.hibernate4.CacheRegion;
19 | import net.oschina.j2cache.hibernate4.strategy.J2CacheAccessStrategyFactory;
20 | import org.hibernate.cache.CacheException;
21 | import org.hibernate.cache.spi.CacheDataDescription;
22 | import org.hibernate.cache.spi.EntityRegion;
23 | import org.hibernate.cache.spi.access.AccessType;
24 | import org.hibernate.cache.spi.access.EntityRegionAccessStrategy;
25 | import org.hibernate.cfg.Settings;
26 |
27 | import java.util.Properties;
28 |
29 | public class J2CacheEntityRegion extends J2CacheTransactionalDataRegion implements EntityRegion {
30 |
31 | public J2CacheEntityRegion(J2CacheAccessStrategyFactory accessStrategyFactory, CacheRegion underlyingCache, Settings settings, CacheDataDescription metadata, Properties properties) {
32 | super(accessStrategyFactory,underlyingCache, settings, metadata, properties);
33 | }
34 |
35 | @Override
36 | public EntityRegionAccessStrategy buildAccessStrategy(AccessType accessType) throws CacheException {
37 | return getAccessStrategyFactory().createEntityRegionAccessStrategy( this, accessType );
38 | }
39 |
40 | }
41 |
--------------------------------------------------------------------------------
/modules/hibernate4/src/net/oschina/j2cache/hibernate4/regions/J2CacheNaturalIdRegion.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2015-2017.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package net.oschina.j2cache.hibernate4.regions;
17 |
18 | import net.oschina.j2cache.hibernate4.CacheRegion;
19 | import net.oschina.j2cache.hibernate4.strategy.J2CacheAccessStrategyFactory;
20 | import org.hibernate.cache.CacheException;
21 | import org.hibernate.cache.spi.CacheDataDescription;
22 | import org.hibernate.cache.spi.NaturalIdRegion;
23 | import org.hibernate.cache.spi.access.AccessType;
24 | import org.hibernate.cache.spi.access.NaturalIdRegionAccessStrategy;
25 | import org.hibernate.cfg.Settings;
26 |
27 | import java.util.Properties;
28 |
29 | public class J2CacheNaturalIdRegion extends J2CacheTransactionalDataRegion implements NaturalIdRegion {
30 |
31 | public J2CacheNaturalIdRegion(J2CacheAccessStrategyFactory accessStrategyFactory, CacheRegion underlyingCache, Settings settings, CacheDataDescription metadata, Properties properties) {
32 | super(accessStrategyFactory, underlyingCache, settings, metadata, properties);
33 | }
34 |
35 | @Override
36 | public NaturalIdRegionAccessStrategy buildAccessStrategy(AccessType accessType) throws CacheException {
37 | return getAccessStrategyFactory().createNaturalIdRegionAccessStrategy(this, accessType);
38 | }
39 | }
--------------------------------------------------------------------------------
/modules/hibernate4/src/net/oschina/j2cache/hibernate4/regions/J2CacheQueryResultsRegion.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2015-2017.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package net.oschina.j2cache.hibernate4.regions;
17 |
18 | import net.oschina.j2cache.hibernate4.CacheRegion;
19 | import net.oschina.j2cache.hibernate4.strategy.J2CacheAccessStrategyFactory;
20 | import org.hibernate.cache.spi.QueryResultsRegion;
21 |
22 | import java.util.Properties;
23 |
24 | public class J2CacheQueryResultsRegion extends J2CacheGeneralDataRegion implements QueryResultsRegion {
25 |
26 | public J2CacheQueryResultsRegion(J2CacheAccessStrategyFactory accessStrategyFactory, CacheRegion underlyingCache, Properties properties) {
27 | super( accessStrategyFactory, underlyingCache, properties );
28 | }
29 |
30 | }
--------------------------------------------------------------------------------
/modules/hibernate4/src/net/oschina/j2cache/hibernate4/regions/J2CacheTimestampsRegion.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2015-2017.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package net.oschina.j2cache.hibernate4.regions;
17 |
18 | import net.oschina.j2cache.hibernate4.CacheRegion;
19 | import net.oschina.j2cache.hibernate4.strategy.J2CacheAccessStrategyFactory;
20 | import org.hibernate.cache.spi.TimestampsRegion;
21 |
22 | import java.util.Properties;
23 |
24 | public class J2CacheTimestampsRegion extends J2CacheGeneralDataRegion implements TimestampsRegion {
25 |
26 | public J2CacheTimestampsRegion(J2CacheAccessStrategyFactory accessStrategyFactory, CacheRegion underlyingCache, Properties properties) {
27 | super(accessStrategyFactory, underlyingCache, properties);
28 | }
29 |
30 | }
31 |
--------------------------------------------------------------------------------
/modules/hibernate4/src/net/oschina/j2cache/hibernate4/strategy/AbstractJ2CacheAccessStrategy.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2015-2017.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package net.oschina.j2cache.hibernate4.strategy;
17 |
18 | import net.oschina.j2cache.hibernate4.regions.J2CacheTransactionalDataRegion;
19 | import org.hibernate.cache.CacheException;
20 | import org.hibernate.cache.spi.access.SoftLock;
21 | import org.hibernate.cfg.Settings;
22 |
23 |
24 | abstract class AbstractJ2CacheAccessStrategy {
25 |
26 | private final T region;
27 | private final Settings settings;
28 |
29 | AbstractJ2CacheAccessStrategy(T region, Settings settings) {
30 | this.region = region;
31 | this.settings = settings;
32 | }
33 |
34 | protected T region() {
35 | return region;
36 | }
37 |
38 | protected Settings settings() {
39 | return settings;
40 | }
41 |
42 | public final boolean putFromLoad(Object key, Object value, long txTimestamp, Object version) throws CacheException {
43 | return putFromLoad( key, value, txTimestamp, version, settings.isMinimalPutsEnabled() );
44 | }
45 |
46 | public abstract boolean putFromLoad(Object key, Object value, long txTimestamp, Object version, boolean minimalPutOverride)
47 | throws CacheException;
48 |
49 | public final SoftLock lockRegion() {
50 | return null;
51 | }
52 |
53 | public final void unlockRegion(SoftLock lock) throws CacheException {
54 | region.clear();
55 | }
56 |
57 | public void remove(Object key) throws CacheException {
58 | }
59 |
60 | public final void removeAll() throws CacheException {
61 | region.clear();
62 | }
63 |
64 | public final void evict(Object key) throws CacheException {
65 | region.remove( key );
66 | }
67 |
68 | public final void evictAll() throws CacheException {
69 | region.clear();
70 | }
71 |
72 | }
73 |
--------------------------------------------------------------------------------
/modules/hibernate4/src/net/oschina/j2cache/hibernate4/strategy/J2CacheAccessStrategyFactory.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2015-2017.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package net.oschina.j2cache.hibernate4.strategy;
17 |
18 | import net.oschina.j2cache.hibernate4.regions.J2CacheCollectionRegion;
19 | import net.oschina.j2cache.hibernate4.regions.J2CacheEntityRegion;
20 | import net.oschina.j2cache.hibernate4.regions.J2CacheNaturalIdRegion;
21 | import org.hibernate.cache.spi.access.AccessType;
22 | import org.hibernate.cache.spi.access.CollectionRegionAccessStrategy;
23 | import org.hibernate.cache.spi.access.EntityRegionAccessStrategy;
24 | import org.hibernate.cache.spi.access.NaturalIdRegionAccessStrategy;
25 |
26 | public interface J2CacheAccessStrategyFactory {
27 |
28 | EntityRegionAccessStrategy createEntityRegionAccessStrategy(J2CacheEntityRegion entityRegion, AccessType accessType);
29 |
30 | CollectionRegionAccessStrategy createCollectionRegionAccessStrategy(J2CacheCollectionRegion collectionRegion, AccessType accessType);
31 |
32 | NaturalIdRegionAccessStrategy createNaturalIdRegionAccessStrategy(J2CacheNaturalIdRegion naturalIdRegion, AccessType accessType);
33 |
34 | }
35 |
--------------------------------------------------------------------------------
/modules/hibernate4/src/net/oschina/j2cache/hibernate4/strategy/NonStrictReadWriteJ2CacheCollectionRegionAccessStrategy.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2015-2017.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package net.oschina.j2cache.hibernate4.strategy;
17 |
18 | import net.oschina.j2cache.hibernate4.regions.J2CacheCollectionRegion;
19 | import org.hibernate.cache.CacheException;
20 | import org.hibernate.cache.spi.CollectionRegion;
21 | import org.hibernate.cache.spi.access.CollectionRegionAccessStrategy;
22 | import org.hibernate.cache.spi.access.SoftLock;
23 | import org.hibernate.cfg.Settings;
24 |
25 |
26 | public class NonStrictReadWriteJ2CacheCollectionRegionAccessStrategy extends AbstractJ2CacheAccessStrategy implements CollectionRegionAccessStrategy {
27 |
28 | public NonStrictReadWriteJ2CacheCollectionRegionAccessStrategy(J2CacheCollectionRegion region, Settings settings) {
29 | super(region, settings);
30 | }
31 |
32 | @Override
33 | public CollectionRegion getRegion() {
34 | return region();
35 | }
36 |
37 | @Override
38 | public Object get(Object key, long txTimestamp) throws CacheException {
39 | return region().get(key);
40 | }
41 |
42 | @Override
43 | public boolean putFromLoad(Object key, Object value, long txTimestamp, Object version, boolean minimalPutOverride)
44 | throws CacheException {
45 | if (minimalPutOverride && region().contains(key)) {
46 | return false;
47 | } else {
48 | region().put(key, value);
49 | return true;
50 | }
51 | }
52 |
53 | @Override
54 | public SoftLock lockItem(Object key, Object version) throws CacheException {
55 | return null;
56 | }
57 |
58 | @Override
59 | public void unlockItem(Object key, SoftLock lock) throws CacheException {
60 | region().remove(key);
61 | }
62 |
63 | @Override
64 | public void remove(Object key) throws CacheException {
65 | region().remove(key);
66 | }
67 | }
68 |
--------------------------------------------------------------------------------
/modules/hibernate4/src/net/oschina/j2cache/hibernate4/strategy/ReadOnlyJ2CacheCollectionRegionAccessStrategy.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2015-2017.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package net.oschina.j2cache.hibernate4.strategy;
17 |
18 |
19 | import net.oschina.j2cache.hibernate4.regions.J2CacheCollectionRegion;
20 | import org.hibernate.cache.CacheException;
21 | import org.hibernate.cache.spi.CollectionRegion;
22 | import org.hibernate.cache.spi.access.CollectionRegionAccessStrategy;
23 | import org.hibernate.cache.spi.access.SoftLock;
24 | import org.hibernate.cfg.Settings;
25 |
26 | public class ReadOnlyJ2CacheCollectionRegionAccessStrategy extends AbstractJ2CacheAccessStrategy implements CollectionRegionAccessStrategy {
27 |
28 | public ReadOnlyJ2CacheCollectionRegionAccessStrategy(J2CacheCollectionRegion region, Settings settings) {
29 | super(region, settings);
30 | }
31 |
32 | @Override
33 | public CollectionRegion getRegion() {
34 | return region();
35 | }
36 |
37 | @Override
38 | public Object get(Object key, long txTimestamp) throws CacheException {
39 | return region().get(key);
40 | }
41 |
42 | @Override
43 | public boolean putFromLoad(Object key, Object value, long txTimestamp, Object version, boolean minimalPutOverride) throws CacheException {
44 | if (minimalPutOverride && region().contains(key)) {
45 | return false;
46 | } else {
47 | region().put(key, value);
48 | return true;
49 | }
50 | }
51 |
52 | @Override
53 | public SoftLock lockItem(Object key, Object version) throws UnsupportedOperationException {
54 | return null;
55 | }
56 |
57 | @Override
58 | public void unlockItem(Object key, SoftLock lock) throws CacheException {
59 | }
60 |
61 | }
--------------------------------------------------------------------------------
/modules/hibernate4/src/net/oschina/j2cache/hibernate4/strategy/ReadWriteJ2CacheCollectionRegionAccessStrategy.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2015-2017.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package net.oschina.j2cache.hibernate4.strategy;
17 |
18 |
19 | import net.oschina.j2cache.hibernate4.regions.J2CacheCollectionRegion;
20 | import org.hibernate.cache.spi.CollectionRegion;
21 | import org.hibernate.cache.spi.access.CollectionRegionAccessStrategy;
22 | import org.hibernate.cfg.Settings;
23 |
24 | public class ReadWriteJ2CacheCollectionRegionAccessStrategy extends AbstractReadWriteJ2CacheAccessStrategy implements CollectionRegionAccessStrategy {
25 |
26 | public ReadWriteJ2CacheCollectionRegionAccessStrategy(J2CacheCollectionRegion region, Settings settings) {
27 | super(region, settings);
28 | }
29 |
30 | @Override
31 | public CollectionRegion getRegion() {
32 | return region();
33 | }
34 |
35 | }
36 |
--------------------------------------------------------------------------------
/modules/hibernate4/src/net/oschina/j2cache/hibernate4/util/TimeProviderLoader.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2015-2017.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package net.oschina.j2cache.session;
17 |
18 | /**
19 | * When cached data expired in ehcache, this listener will be invoked.
20 | *
21 | * @author Winter Lau(javayou@gmail.com)
22 | */
23 | public interface CacheExpiredListener {
24 |
25 | void notifyElementExpired(String key) ;
26 |
27 | }
28 |
--------------------------------------------------------------------------------
/modules/session-manager/src/main/java/net/oschina/j2cache/session/CaffeineCache.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2015-2017, Winter Lau (javayou@gmail.com).
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package net.oschina.j2cache.session;
17 |
18 | import com.github.benmanes.caffeine.cache.Cache;
19 | import com.github.benmanes.caffeine.cache.Caffeine;
20 | import com.github.benmanes.caffeine.cache.RemovalCause;
21 |
22 | import java.util.concurrent.TimeUnit;
23 |
24 | /**
25 | * Caffeine cache
26 | *
27 | * @author Winter Lau(javayou@gmail.com)
28 | */
29 | public class CaffeineCache {
30 |
31 | private Cache cache;
32 | private int size ;
33 | private int expire ;
34 |
35 | public CaffeineCache(int size, int expire, CacheExpiredListener listener) {
36 | cache = Caffeine.newBuilder()
37 | .maximumSize(size)
38 | .expireAfterAccess(expire, TimeUnit.SECONDS)
39 | .removalListener((k,v, cause) -> {
40 | //程序删除的缓存不做通知处理,因为上层已经做了处理
41 | if(cause != RemovalCause.EXPLICIT && cause != RemovalCause.REPLACED)
42 | listener.notifyElementExpired((String)k);
43 | })
44 | .build();
45 |
46 | this.size = size;
47 | this.expire = expire;
48 | }
49 |
50 | public Object get(String session_id) {
51 | return cache.getIfPresent(session_id);
52 | }
53 |
54 | public void put(String session_id, Object value) {
55 | cache.put(session_id, value);
56 | }
57 |
58 | public void evict(String session_id) {
59 | cache.invalidate(session_id);
60 | }
61 |
62 | public void close() {
63 | }
64 |
65 | public int getSize() {
66 | return size;
67 | }
68 |
69 | public int getExpire() {
70 | return expire;
71 | }
72 | }
73 |
--------------------------------------------------------------------------------
/modules/session-manager/src/main/java/net/oschina/j2cache/session/Serializer.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2015-2017, Winter Lau (javayou@gmail.com).
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *