> ## Documentation Index
> Fetch the complete documentation index at: https://docs.sajuapi.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# 캐시 삭제

특정 사용자의 캐시된 운세 데이터를 삭제합니다.

프로필 삭제 시 또는 운세 재생성이 필요할 때 사용합니다.

***

## Request Body

<ParamField body="userName" type="string" required>
  캐시를 삭제할 사용자 이름입니다.
</ParamField>

***

## Response

### 성공

캐시 삭제에 성공하면 삭제된 항목 수가 반환됩니다.

***

## 요청 예시

<CodeGroup>
  ```javascript JavaScript theme={null}
  const response = await fetch('/api/clear-cache', {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({
      userName: '김철수'
    })
  });

  const result = await response.json();
  console.log(`삭제된 캐시: ${result.deletedCount}개`);
  ```

  ```python Python theme={null}
  import requests

  response = requests.post(
      'https://sajuapi.dev/api/clear-cache',
      json={
          'userName': '김철수'
      }
  )

  result = response.json()
  print(f"삭제된 캐시: {result['deletedCount']}개")
  ```

  ```bash cURL theme={null}
  curl -X POST https://sajuapi.dev/api/clear-cache \
    -H "Content-Type: application/json" \
    -d '{"userName": "김철수"}'
  ```
</CodeGroup>

***

## 응답 예시

```json theme={null}
{
  "success": true,
  "deletedCount": 15,
  "deletedKeys": [
    "fortune:김철수:2025-01-15:haiku",
    "fortune:김철수:2025-01-16:haiku",
    "character:김철수:haiku",
    "tendency:김철수:haiku",
    "radar:김철수:haiku"
  ]
}
```

***

## 삭제되는 캐시

| 캐시 유형     | 키 패턴                                | 설명            |
| --------- | ----------------------------------- | ------------- |
| Fortune   | `fortune:{userName}:{date}:{model}` | 일일 운세 (최근 7일) |
| Character | `character:{userName}:{model}`      | 투자 성격 분석      |
| Tendency  | `tendency:{userName}:{model}`       | 투자 성향 분석      |
| Radar     | `radar:{userName}:{model}`          | 레이더 차트 데이터    |

***

## 사용 사례

### 프로필 삭제 시

사용자가 프로필을 삭제하면 관련 캐시도 함께 삭제해야 합니다.

```javascript theme={null}
async function deleteProfile(profileId, userName) {
  // 1. 로컬 스토리지에서 프로필 삭제
  const profiles = JSON.parse(localStorage.getItem('saju_profiles') || '{}');
  delete profiles[profileId];
  localStorage.setItem('saju_profiles', JSON.stringify(profiles));

  // 2. 서버 캐시 삭제 (fire-and-forget)
  fetch('/api/clear-cache', {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ userName })
  }).catch(() => {
    // 실패해도 무시 (캐시는 자연스럽게 만료됨)
  });
}
```

### 운세 강제 재생성

사용자가 운세를 다시 생성하고 싶을 때 캐시를 삭제합니다.

```javascript theme={null}
async function regenerateFortune(userName, date, model) {
  // 1. 캐시 삭제
  await fetch('/api/clear-cache', {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ userName })
  });

  // 2. 새로운 운세 생성
  const fortune = await fetch('/api/daily-fortune', {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ saju, userName, date, model })
  });

  return fortune.json();
}
```

<Note>
  캐시는 Upstash Redis에 저장됩니다. 캐시 삭제 실패 시에도 자연스럽게 TTL에 의해 만료됩니다.
</Note>

<Warning>
  모든 모델(haiku, sonnet, gpt4o, gemini)의 캐시가 함께 삭제됩니다.
</Warning>
