A REST API service for storing, retrieving, and searching image metadata using Redis as the backend storage and caching layer.
- Fast Storage & Retrieval: Store and retrieve image metadata with Redis hash structures
- Advanced Search: Search images by tags, format, and size with intelligent caching
- Performance Optimized: Built-in search result caching with automatic cache invalidation
- Statistics Dashboard: Real-time service statistics including memory usage
- RESTful API: Clean HTTP endpoints for all operations
- Go 1.25.1 or higher
- Redis server running on
localhost:6379
- Clone the repository:
git clone https://github.com/ajitashwath/image-metadata.git
cd image-metadata- Install dependencies:
go mod tidy- Start Redis server (in WSL):
redis-server- Run the service:
go run main.goThe service will start on http://localhost:8080
POST /images
Store metadata for a new image.
Request Body:
{
"id": "img001",
"name": "sunset.jpg",
"tags": ["nature", "sunset", "landscape"],
"size": 1024000,
"format": "jpg",
"width": 1920,
"height": 1080
}Response:
{
"status": "stored"
}GET /images/{id}
Retrieve metadata for a specific image.
Response:
{
"id": "img001",
"name": "sunset.jpg",
"tags": ["nature", "sunset", "landscape"],
"size": 1024000,
"format": "jpg",
"width": 1920,
"height": 1080
}GET /search?q={query}
Search for images using various criteria.
Query Formats:
tag:nature- Search by tagformat:jpg- Search by formatsize:>500000- Search by size (greater than)size:<2000000- Search by size (less than)
Response:
{
"query": "tag:nature",
"count": 2,
"results": [
{
"id": "img001",
"name": "sunset.jpg",
"tags": ["nature", "sunset"],
"size": 1024000,
"format": "jpg",
"width": 1920,
"height": 1080
}
]
}GET /stats
Get service statistics and Redis memory usage.
Response:
{
"total_images": 150,
"cached_searches": 25,
"redis_memory_usage": "2.5M"
}curl -X POST http://localhost:8080/images \
-H "Content-Type: application/json" \
-d '{
"id": "img001",
"name": "mountain.jpg",
"tags": ["nature", "mountain", "landscape"],
"size": 2048000,
"format": "jpg",
"width": 2560,
"height": 1440
}'curl "http://localhost:8080/search?q=tag:nature"curl "http://localhost:8080/search?q=format:png"# Images larger than 1MB
curl "http://localhost:8080/search?q=size:>1000000"
# Images smaller than 500KB
curl "http://localhost:8080/search?q=size:<500000"curl http://localhost:8080/images/img001curl http://localhost:8080/stats- Image Metadata: Stored as Redis hashes with key pattern
image:{id} - Tag Indexes: Redis sets with key pattern
tag:{tagname}containing image IDs - Format Indexes: Redis sets with key pattern
format:{format}containing image IDs
- Search results are cached for 5 minutes using key pattern
search:{query} - Cache is automatically invalidated when new images with matching tags/formats are stored
- Cache hits and misses are logged for monitoring
- Indexed Searches: Fast
O(1)lookups for tags and formats using Redis sets - Intelligent Caching: Search results cached with automatic invalidation
- Bulk Operations: Efficient batch processing for search results
- Memory Monitoring: Real-time Redis memory usage tracking
The service uses the following default Redis configuration:
- Host:
localhost - Port:
6379 - Database:
0
To modify these settings, update the NewService() function in main.go.
The API returns appropriate HTTP status codes:
200 OK- Successful operations400 Bad Request- Invalid request format or query404 Not Found- Image not found500 Internal Server Error- Server-side errors
- gorilla/mux: HTTP router for REST endpoints
- go-redis/redis/v8: Redis client for data storage
- Standard Library: JSON encoding, HTTP handling, logging
go test ./...go build -o image-service main.go
./image-serviceThe service provides built-in monitoring through:
- Request logging for cache hits/misses
- Redis memory usage statistics
- Image count and cached search metrics
Access monitoring data via the /stats endpoint or check application logs.