Commit f1611dcfb301627e7dcbf3c5dc1cd6b82a053bac
Committed by
Andrew Shvayka
1 parent
bc8c58c3
created TbRedisLwM2MClientStore
Showing
2 changed files
with
64 additions
and
1 deletions
... | ... | @@ -59,7 +59,7 @@ public class TbLwM2mStoreFactory { |
59 | 59 | |
60 | 60 | @Bean |
61 | 61 | private TbLwM2MClientStore clientStore() { |
62 | - return new TbDummyLwM2MClientStore(); | |
62 | + return isRedis() ? new TbRedisLwM2MClientStore(getConnectionFactory()) : new TbDummyLwM2MClientStore(); | |
63 | 63 | } |
64 | 64 | |
65 | 65 | @Bean | ... | ... |
1 | +/** | |
2 | + * Copyright © 2016-2021 The Thingsboard Authors | |
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 | + * | |
8 | + * http://www.apache.org/licenses/LICENSE-2.0 | |
9 | + * | |
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 org.thingsboard.server.transport.lwm2m.server.store; | |
17 | + | |
18 | +import org.nustaq.serialization.FSTConfiguration; | |
19 | +import org.springframework.data.redis.connection.RedisConnectionFactory; | |
20 | +import org.thingsboard.server.transport.lwm2m.server.client.LwM2mClient; | |
21 | + | |
22 | +public class TbRedisLwM2MClientStore implements TbLwM2MClientStore { | |
23 | + | |
24 | + private static final String CLIENT_EP = "CLIENT#EP#"; | |
25 | + private final RedisConnectionFactory connectionFactory; | |
26 | + private final FSTConfiguration serializer; | |
27 | + | |
28 | + public TbRedisLwM2MClientStore(RedisConnectionFactory redisConnectionFactory) { | |
29 | + this.connectionFactory = redisConnectionFactory; | |
30 | + this.serializer = FSTConfiguration.createDefaultConfiguration(); | |
31 | + } | |
32 | + | |
33 | + @Override | |
34 | + public LwM2mClient get(String endpoint) { | |
35 | + try (var connection = connectionFactory.getConnection()) { | |
36 | + byte[] data = connection.get(getKey(endpoint)); | |
37 | + if (data == null) { | |
38 | + return null; | |
39 | + } else { | |
40 | + return (LwM2mClient) serializer.asObject(data); | |
41 | + } | |
42 | + } | |
43 | + } | |
44 | + | |
45 | + @Override | |
46 | + public void put(LwM2mClient client) { | |
47 | + byte[] clientSerialized = serializer.asByteArray(client); | |
48 | + try (var connection = connectionFactory.getConnection()) { | |
49 | + connection.getSet(getKey(client.getEndpoint()), clientSerialized); | |
50 | + } | |
51 | + } | |
52 | + | |
53 | + @Override | |
54 | + public void remove(String endpoint) { | |
55 | + try (var connection = connectionFactory.getConnection()) { | |
56 | + connection.del(getKey(endpoint)); | |
57 | + } | |
58 | + } | |
59 | + | |
60 | + private byte[] getKey(String endpoint) { | |
61 | + return (CLIENT_EP + endpoint).getBytes(); | |
62 | + } | |
63 | +} | ... | ... |