Skip to content

Feature | Authenticator with cache#3

Merged
nicogaldamez merged 6 commits intomainfrom
feature/cache-authenticator
Feb 11, 2025
Merged

Feature | Authenticator with cache#3
nicogaldamez merged 6 commits intomainfrom
feature/cache-authenticator

Conversation

@bruno-costanzo
Copy link
Contributor

Authentication System with Cache

Why

  • Reduce API calls to Uber's auth service
  • Make the gem more efficient by caching tokens
  • Allow flexibility for different cache solutions (Redis, Memcached, etc)

What

  • Added token caching system with:
    • access_token method to get cached token or fetch a new one
    • refresh_access_token to force new token fetch
    • Automatic token expiration based on Uber's response
    • Configurable cache implementation

@bruno-costanzo bruno-costanzo force-pushed the feature/cache-authenticator branch from b494701 to 80568f5 Compare February 10, 2025 13:03
README.md Outdated
Comment on lines +24 to +26
Ruber uses the `Ruber::Cache` class to store the access token. The cache is a simple in-memory cache that is cleared when the access token expires.

You can configure your own cache implementation by setting the `Ruber.cache` attribute.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Qué te parece decir que es para manejar la cache en lugar de el access token? El access token termina siendo un ejemplo de uso de la cache.
Además podríamos indicar cual es el uso por default.

Suggested change
Ruber uses the `Ruber::Cache` class to store the access token. The cache is a simple in-memory cache that is cleared when the access token expires.
You can configure your own cache implementation by setting the `Ruber.cache` attribute.
Ruber uses a caching solution to improve efficiency (e.g., for caching tokens). By default, it uses [método por default], but you can change the cache method by setting the Ruber.cache attribute.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

perfecto. Esto no lo pensé demasiado porque me pareció que lo importante era dejarlo escrito en algún lado y después revisar bien todo el readme en algún momento

Comment on lines +5 to +9
def read(key) = memory_store[key]
def write(key, value, _options = {}) = memory_store[key] = value
def clear = memory_store.clear
def delete(key) = memory_store.delete(key)
def memory_store = @memory_store ||= {}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Está bien que el método por defecto sea cachear en memoria? No conviene que sea un FileStore? Porque si se usa memoria se va a pedir el token en cada vuelta y Uber podría penalizarte o que superes la quota.

Entiendo que la idea es que los usuarios indiquen cual es su cache store de preferencia, pero me parece que estaría bueno que ya venga un store útil por defecto.

Qué decís?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

en realidad primero había hecho uno que devolvía todo nil y listo, pero para los tests esto es conveniente porque funciona como si tuvieras algún caché. No estoy seguro de qué convenga hacer por defecto.

Comment on lines +5 to +9

module Ruber
class Authenticator
OAUTH_URL = "https://auth.uber.com/oauth/v2/token"
CACHE_KEY = "uber_auth_token"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Este se usa? Creo que está inicializado pero luego se usa Ruber.cache_key.

Me pregunto si no conviene dejar la cache_key acá en lugar de tenerla en Configuration. No te parece mejor dejar en Configuration solo cosas que el usuario podría querer setear?


class << self
def access_token
Ruber.cache.read(Ruber.cache_key) || fetch_new_token
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No debería llamar a refresh_access_token si expiró?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

el refresh token lo pensé más como si por ejemplo en la caché tenés un token y cuando haces el llamado a uber te tira error o algo, entonces vas a hacer un refresh anticipado del token (por eso tmb hace un delete del cache key primero). No estoy seguro de qué conviene.

@bruno-costanzo bruno-costanzo force-pushed the feature/cache-authenticator branch from d38e5bd to 3a815d2 Compare February 10, 2025 13:23
Ruber.cache.read(Ruber.cache_key) || fetch_new_token
@access_token = cached_token || fetch_new_token

@access_token = refresh_access_token if token_expired?
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

en qué contexto puede pasar que el token esté expirado acá?

se supone que cached_token guarda el token en caché por el tiempo de expiración que te devuelve la misma API, o no?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah ahí vi que también modificaste el write del caché para que el token quede guardado

@nicogaldamez nicogaldamez merged commit 4a4fbcf into main Feb 11, 2025
1 check passed
@nicogaldamez nicogaldamez deleted the feature/cache-authenticator branch February 11, 2025 12:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants