0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

.NETのお勉強4_redisコンテナと連携

Last updated at Posted at 2025-01-29

概要

AmazonLinux2023インスタンス上にコンテナで.NETを利用したサーバをたてる
そのお勉強メモ4

redisコンテナと連携する

連携の前にmysqlコンテナ単体たてる

最低限がこんな感じ
データの永続化のためvolumesで外にも出す

services:
  redis:
    image: redis:7.4.2
    ports:
      - "6379:6379"
    volumes:
      - ./redis:/data

ソース

src/MyServerApp.csproj
<Project Sdk="Microsoft.NET.Sdk.Web">
  <PropertyGroup>
    <TargetFramework>net9.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="MySql.Data" Version="8.1.0" />
    <PackageReference Include="Microsoft.Extensions.Caching.StackExchangeRedis" Version="8.0.0" />
  </ItemGroup>
</Project>
src/Controller/RedisController.cs
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Distributed;

[ApiController]
[Route("[controller]")]
public class RedisController : ControllerBase
{
    private readonly IDistributedCache _cache;

    public RedisController(IDistributedCache cache)
    {
        _cache = cache;
    }

    [HttpGet("set/{key}/{value}")]
    public IActionResult Set(string key, string value)
    {
        var options = new DistributedCacheEntryOptions().SetAbsoluteExpiration(TimeSpan.FromMinutes(10));
        _cache.SetString(key, value, options);
        return Ok($"SET {key} = {value}");
    }

    [HttpGet("get/{key}")]
    public IActionResult Get(string key)
    {
        var value = _cache.GetString(key);
        if (string.IsNullOrEmpty(value)) {
            return NotFound($"'{key}' not found");
        }
        return Ok($"GET {key} = {value}");
    }
}
src/Program.cs
var builder = WebApplication.CreateBuilder(args);

builder.Services.AddStackExchangeRedisCache(options =>
{
    options.Configuration = "redis:6379";
});
builder.Services.AddControllers();

var app = builder.Build();
app.MapGet("/", () => "Hello, World!");
app.MapControllers();
app.Run();
docker-compose.hotload.yml
services:
  build:
    image: builder
    build:
      context: .
      dockerfile: Dockerfile.build
    volumes:
      - ./src:/src
    ports:
      - "80:8080"
    command: dotnet watch run --urls=http://0.0.0.0:8080
  mysql:
    image: mysql:8.0
    environment:
      MYSQL_ROOT_PASSWORD: root_password
      MYSQL_USER: user
      MYSQL_PASSWORD: password
      MYSQL_DATABASE: cs_test
    ports:
      - "3306:3306"
    volumes:
      - ./mysql:/var/lib/mysql
  redis:
    image: redis:7.4.2
    ports:
      - "6379:6379"
    volumes:
      - ./redis:/data

実行テスト

xx.xx.xx.xx/redis/set/key1/value1アクセス後、redisコンテナ内でredis-cliで入ってデータが入っており、xx.xx.xx.xx/redis/get/key1アクセスでvalue1が表示されればok

その他メモ

あとで勉強して追記
・redis系の処理はasyncすべき?
・redisはクラスターで使いたい

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?