Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ target/
!**/src/test/**/target/

### IntelliJ IDEA ###
.idea/modules.xml
.idea/jarRepositories.xml
.idea/compiler.xml
.idea/libraries/
.idea
src/main/resources/application.yaml
*.iws
*.iml
*.ipr
Expand Down
8 changes: 0 additions & 8 deletions .idea/.gitignore

This file was deleted.

7 changes: 0 additions & 7 deletions .idea/encodings.xml

This file was deleted.

6 changes: 0 additions & 6 deletions .idea/jpa-buddy.xml

This file was deleted.

15 changes: 0 additions & 15 deletions .idea/misc.xml

This file was deleted.

6 changes: 0 additions & 6 deletions .idea/vcs.xml

This file was deleted.

5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@
<version>5.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
<version>2.2</version>
</dependency>
</dependencies>

</project>
53 changes: 52 additions & 1 deletion src/main/java/org/redis/Main.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,58 @@
package org.redis;

import org.redis.config.RedisConfig;
import org.redis.service.RedisMap;
import redis.clients.jedis.JedisPool;

public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
RedisConfig config = new RedisConfig();
System.out.println("Connecting to Redis with config: " + config);

JedisPool jedisPool = config.createJedisPool();

// Создание экземпляра RedisMap
RedisMap redisMap = new RedisMap(jedisPool, "test");

System.out.println("Redis Map Demo");
System.out.println("==============");

// put операции
redisMap.put("name", "John Doe");
redisMap.put("email", "john@example.com");
redisMap.put("city", "Moscow");

System.out.println("After put operations:");
System.out.println("Size: " + redisMap.size());
System.out.println("Get name: " + redisMap.get("name"));

// containsKey и containsValue
System.out.println("\nContains key 'email': " + redisMap.containsKey("email"));
System.out.println("Contains value 'Moscow': " + redisMap.containsValue("Moscow"));

// keySet и values
System.out.println("\nKey set: " + redisMap.keySet());
System.out.println("Values: " + redisMap.values());

// remove
String removed = redisMap.remove("city");
System.out.println("\nRemoved city: " + removed);
System.out.println("Size after remove: " + redisMap.size());

// putAll
java.util.Map<String, String> newEntries = new java.util.HashMap<>();
newEntries.put("phone", "123-456-789");
newEntries.put("age", "30");
redisMap.putAll(newEntries);

System.out.println("\nAfter putAll:");
System.out.println("All entries: " + redisMap.entrySet());

// clear
redisMap.clear();
System.out.println("\nAfter clear, is empty: " + redisMap.isEmpty());

// Закрытие пула соединений
jedisPool.close();
}
}
132 changes: 132 additions & 0 deletions src/main/java/org/redis/config/RedisConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
package org.redis.config;

import org.yaml.snakeyaml.Yaml;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.UncheckedIOException;
import java.util.Collections;
import java.util.Map;

public class RedisConfig {

private static final Map<String, Object> YAML_REDIS = loadRedisSection();

private final String host;
private final int port;
private final String password;
private final int timeout;

public RedisConfig() {
this.host = getString("REDIS_HOST", "host", "localhost");
this.port = getInt("REDIS_PORT", "port", 6379);
this.password = getPassword();
this.timeout = getInt("REDIS_TIMEOUT", "timeout", 2000);
}

public boolean hasPassword() {
return password != null && !password.isEmpty();
}

public JedisPool createJedisPool() {
JedisPoolConfig poolConfig = new JedisPoolConfig();
poolConfig.setMaxTotal(10);
poolConfig.setMaxIdle(5);
poolConfig.setMinIdle(1);

if (hasPassword()) {
return new JedisPool(poolConfig, host, port, timeout, password);
}

return new JedisPool(poolConfig, host, port, timeout);
}

@Override
public String toString() {
return "RedisConfig{" +
"host='" + host + '\'' +
", port=" + port +
", hasPassword=" + hasPassword() +
", timeout=" + timeout +
'}';
}

@SuppressWarnings("unchecked")
private static Map<String, Object> loadRedisSection() {
try (InputStream is = RedisConfig.class.getClassLoader().getResourceAsStream("application.yaml")) {
if (is == null) {
throw new FileNotFoundException("application.yaml not found");
}
Yaml yaml = new Yaml();
Map<String, Object> root = yaml.load(is);

if (root == null) {
return Collections.emptyMap();
}

Object redis = root.get("redis");
if (redis instanceof Map<?, ?> m) {
return (Map<String, Object>) m;
}

return Collections.emptyMap();
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}

private static String getString(String envKey, String yamlKey, String defaultValue) {
String envValue = System.getenv(envKey);
if (envValue != null && !envValue.isEmpty()) {
return envValue;
}

Object yamlVal = YAML_REDIS.get(yamlKey);
if (yamlVal == null) {
return defaultValue;
}

if (yamlVal instanceof String s) {
return s.isEmpty() ? defaultValue : s;
}

return String.valueOf(yamlVal);
}

private static int getInt(String envKey, String yamlKey, int defaultValue) {
String envValue = System.getenv(envKey);
if (envValue != null && !envValue.isEmpty()) {
return Integer.parseInt(envValue);
}

Object yamlVal = YAML_REDIS.get(yamlKey);
if (yamlVal instanceof Number n) {
return n.intValue();
}

if (yamlVal instanceof String s && !s.isEmpty()) {
return Integer.parseInt(s);
}

return defaultValue;
}

private static String getPassword() {
String envValue = System.getenv("REDIS_PASSWORD");
if (envValue != null && !envValue.isEmpty()) {
return envValue;
}

Object yamlVal = YAML_REDIS.get("password");
if (yamlVal == null) {
return null;
}

String s = yamlVal instanceof String ? (String) yamlVal : String.valueOf(yamlVal);

return s.isEmpty() ? null : s;
}
}
Loading