Spring Boot ile Redis
Bu yazıda Redis'i tartışacağız , Redis , birden çok amaç için kullanılabilen, Anahtar-değer tabanlı bir NoSQL veritabanıdır. Açık kaynaklıdır ve bir InMemory veri yapısı deposudur. Çünkü Redis, list, set, map, sorted set gibi tüm temel veri yapılarını destekler.
Redis'i kullanarak sohbet uygulamaları, oturum depolama uygulamaları, oyun kontrol panelleri vb. gibi farklı türde uygulamalar geliştirebiliriz.
Redis'i öğrenmek istiyorsanız, önce Redis'in desteklediği farklı veri yapılarını, Redis'ten veri depolayıp alabileceğimiz komutların dışında anlayın.
Bazı Önemli Komutlar
Redis, anahtar — değerler olarak String, Set, Sorted Set, List ve HashMap'i destekler. Aşağıda, farklı veri yapılarını anlamak için bazı temel komutlar verilmiştir.
String
SET foo bar (foo is key, bar is value)
GET foo
bar
HashMap
HMSET student name: "shubham" age: 25
HGETALL student
List
LPUSH product car
LPUSH product bike
LRANGE product 0 10
Set
SADD product car
SADD product bike
SMEMBERS product
Redis Kullanım Durumları
- Redis, Anahtar-değer tabanlı bir NoSQL veritabanı olarak kullanılabilir.
- Redis önbellek sağlayıcı olarak kullanılabilir.
- Redis, olay işlemede kullanılan yayıncı ve abone olarak kullanılabilir.
- Redis bir oturum deposu olarak kullanılabilir.
- Redis, sohbet uygulamalarında kullanılabilir.
Dolayısıyla, bunu okuduktan sonra biraz ilgi görürseniz ve şimdi Redis üzerinde uygulamalı pratik yapmak istiyorsanız, sisteminizde Redis sunucusunu kurun.
- Orada değilse, sisteminize homebrew yükleyin.
- Sırayla Aşağıda Çalıştır komutları
brew install redis
After succesfull installation
brew services start redis
After starting redis if you want to try above commands
redis-cli
To test whether redis server is working
PING
If yu get PONG then its connected
If you want to monitor which all commands are getting executed on redis
redis-monitor
Bundan sonra, Redis'i başka bir sistemden kullanmak istiyorsanız Redis sunucusuyla etkileşime girecek bir istemciye ihtiyacınız var.
Şimdi bu bölümde Redis'i spring boot ile nasıl kullanabileceğimizi ve NoSQL DB olarak nasıl kullanabileceğimizi göreceğiz.
Kurmak
İlk adım, spring başlatıcıdan örnek bir Spring boot projesi oluşturmak ve spring-boot-starter-data-redis Dependency eklemektir .
Şimdi, projeyi favori IDE'nize aktardıktan sonra paketler ve sınıflar oluşturun.
Birinci sınıf yapılandırma için olacak, Redis için iki sürücümüz var, biri Jedis, diğeri marul (Tembel bir şekilde başlatıldı ve performans açısından da daha iyi.)
Sürücü dışında Redis sunucusunda işlemler yapmak için bir şablona ihtiyacımız var, dinlenme işlemlerini yapmak için RestTemplate'e sahip olmamıza benzer şekilde.
package com.redisexample.redisdemo.config;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.repository.configuration.EnableRedisRepositories;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
@Configuration
//@EnableRedisRepositories
public class AppConfig {
//There are two ways of creating driver, one is jedis another is lettuce,
//lettuce is bit lazy in intialization so it creates beans lazily
//It is using default host and port
@Bean
RedisConnectionFactory jedisConnectionFactory() {
return new LettuceConnectionFactory();
}
@Bean
public RedisTemplate<String, Object> redisTemplate() {
RedisTemplate<String, Object> template = new RedisTemplate<>();
template.setConnectionFactory(jedisConnectionFactory());
template.setKeySerializer(new StringRedisSerializer());
template.setHashKeySerializer(new StringRedisSerializer());
template.setHashKeySerializer(new JdkSerializationRedisSerializer());
template.setValueSerializer(new JdkSerializationRedisSerializer());
template.setEnableTransactionSupport(true);
template.afterPropertiesSet();
return template;
}
//For setting host and port
// @Bean
// JedisConnectionFactory jedisConnectionFactory() {
// JedisConnectionFactory jedisConFactory
// = new JedisConnectionFactory();
// jedisConFactory.setHostName("localhost");
// jedisConFactory.setPort(6379);
// return jedisConFactory;
// }
}
Entity sınıfı oluşturma
@RedisHash("student") // this is a set so we can use set command to see data via redis cli
public class Student {
public enum Gender {
MALE, FEMALE
}
private Long id;
private String name;
private int age;
private String city;
//getters and setters
}
@Repository
public interface StudnetRepo extends CrudRepository<Student, Long> {
}
package com.redisexample.redisdemo.controller;
import com.redisexample.redisdemo.model.Student;
import com.redisexample.redisdemo.repo.StudnetRepo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/student")
public class StudentController {
@Autowired
private StudnetRepo studnetRepo;
@PostMapping
public void saveStudent(@RequestBody Student student){
studnetRepo.save(student);
}
@GetMapping
public Iterable<Student> getStudent(){
return studnetRepo.findAll();
}
}
Bu, Redis'i bir yay önyükleme uygulamasıyla bir DB olarak kullanmanın basit bir örneğidir.
Bir sonraki gönderide, Redis'i önbellek Sağlayıcı olarak nasıl kullanacağımızı göreceğiz.
Okuduğunuz için teşekkürler!!

![Bağlantılı Liste Nedir? [Bölüm 1]](https://post.nghiatu.com/assets/images/m/max/724/1*Xokk6XOjWyIGCBujkJsCzQ.jpeg)



































